1 Star 0 Fork 621

旧光影里的少年/DesignPattern

forked from Micooz/DesignPattern 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
InterpreterPattern.cpp 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
Micooz 提交于 2014-10-09 11:46 +08:00 . 加入解释器模式,迭代器模式
#include <iostream>
#include <string>
using namespace std;
class Context { };
class Expression {
public:
virtual ~Expression() { }
virtual void Interpret(const Context& c) = 0;
};
class TerminalExpression :public Expression {
public:
TerminalExpression(const string& statement) {
_statement = statement;
}
void Interpret(const Context& c) {
cout << this->_statement << " -- TerminalExpression" << endl;
}
private:
string _statement;
};
class NonterminalExpression :public Expression {
public:
NonterminalExpression(Expression* expression, int times) {
_expression = expression;
_times = times;
}
void Interpret(const Context& c) {
for (int i = 0; i < _times; i++) {
_expression->Interpret(c);
}
}
private:
Expression *_expression;
int _times;
};
int main() {
Context *c = new Context();
Expression *tp = new TerminalExpression("echo");
Expression *ntp = new NonterminalExpression(tp, 4);
ntp->Interpret(*c);
delete ntp;
delete tp;
delete c;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yanjun_coder/DesignPattern.git
git@gitee.com:yanjun_coder/DesignPattern.git
yanjun_coder
DesignPattern
DesignPattern
master

搜索帮助