听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • Python

  • Go

  • 数据结构与算法

  • 设计模式

    • 【设计模式】1 引言
    • 创建型

    • 结构型

    • 行为型

      • 【设计模式】行为型模式-Chain of Responsibility模式
      • 【设计模式】行为型模式-Command模式
      • 【设计模式】行为型模式-Interpreter模式
      • 【设计模式】行为型模式-Iterator模式
      • 【设计模式】行为型模式-Mediator模式
      • 【设计模式】行为型模式-Memento模式
      • 【设计模式】行为型模式-Observer模式
      • 【设计模式】行为型模式-State模式
      • 【设计模式】行为型模式-Strategy模式
      • 【设计模式】行为型模式-Template模式
      • 【设计模式】行为型模式-Visitor模式
  • 程序设计层
  • 设计模式
  • 行为型
zfprotectors
2022-05-18

【设计模式】行为型模式-Interpreter模式

# 功能

给定一个语言,定义它的文法表示,并定义一个解释器,这个解释器使用该标识来解释语言中的句子

# 解决

  • 主要解决:对于一些固定文法构建一个解释句子的解释器
  • 何时使用:如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的句子。这样就可以构建一个解释器,该解释器通过解释这些句子来解决该问题。
  • 如何解决:构建语法树,定义终结符与非终结符
  • 关键代码:构建环境类,包含解释器之外的一些全局信息,一般是 HashMap

# 优缺点

  • 优点:
    • 可扩展性比较好,灵活
    • 增加了新的解释表达式的方式
    • 易于实现简单文法
  • 缺点:
    • 可利用场景比较少
    • 对于复杂的文法比较难维护
    • 解释器模式会引起类膨胀
    • 解释器模式采用递归调用方法

# 应用场景

  • 应用实例:编译器、运算表达式计算
  • 使用场景:
    • 可以将一个需要解释执行的语言中的句子表示为一个抽象语法树
    • 一些重复出现的问题可以用一种简单的语言来进行表达
    • 一个简单语法需要解释的场景
  • 注意事项:可利用场景比较少,JAVA 中如果碰到可以用 expression4J 代替

# 简单示例代码部分

Interpreter.h


#ifndef _INTERPRET_H_
#define _INTERPRET_H_
#include <string>
#include <iostream>
using namespace std;
class Context{
public:
    Context(){}
    ~Context(){}
};
class AbstractExpression{
public:
    virtual ~AbstractExpression(){}
    virtual void Interpret(const Context& c){}
protected:
    AbstractExpression(){}
};
class TerminalExpression:public AbstractExpression{
public:
    TerminalExpression(const string& statement){
        this->_statement=statement;
    }
    ~ TerminalExpression(){}
    void Interpret(const Context& c){
        cout<<this->_statement<<"TerminalExpression"<<endl;
    }
private:
    string _statement;
};
class NonterminalExpression:public AbstractExpression{
public:
    NonterminalExpression(AbstractExpression* expression,int times){
        this->_expression = expression;
        this->_times = times;
    }
    ~ NonterminalExpression(){}
    void Interpret(const Context& c){
        for (int i = 0; i < _times ; i++){
            this->_expression->Interpret(c);
        }
    }
private:
    AbstractExpression* _expression;
    int _times;
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

main.cpp


#include "Interpret.h"
#include <iostream>
using namespace std;
int main(){
    Context* c = new Context();
    AbstractExpression* te = new TerminalExpression("hello");
    AbstractExpression* nte = new NonterminalExpression(te,2);
    nte->Interpret(*c);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
#设计模式
上次更新: 2022/05/18, 15:47:37
【设计模式】行为型模式-Command模式
【设计模式】行为型模式-Iterator模式

← 【设计模式】行为型模式-Command模式 【设计模式】行为型模式-Iterator模式→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式