标签:style blog http color os io ar for art
【1】什么是解释器模式?
解释器模式:
【2】解释器模式代码示例:
代码示例:
1 #include <iostream> 2 #include <list> 3 #include <string> 4 using namespace std; 5 6 class Context; 7 8 class AbstractExpression 9 { 10 public: 11 virtual void interpret(Context *) = 0; 12 }; 13 14 class TerminalExpression : public AbstractExpression 15 { 16 public: 17 void interpret(Context *context) 18 { 19 cout << "终端解释器" << endl; 20 } 21 }; 22 23 class NonterminalExpression : public AbstractExpression 24 { 25 public: 26 void interpret(Context *context) 27 { 28 cout << "非终端解释器" << endl; 29 } 30 }; 31 32 class Context 33 { 34 public: 35 string input, output; 36 }; 37 38 int main() 39 { 40 Context *context = new Context(); 41 list<AbstractExpression*> lt; 42 lt.push_back(new TerminalExpression()); 43 lt.push_back(new NonterminalExpression()); 44 lt.push_back(new TerminalExpression()); 45 lt.push_back(new TerminalExpression()); 46 47 for (list<AbstractExpression*>::iterator iter = lt.begin(); iter != lt.end(); iter++) 48 { 49 (*iter)->interpret(context); 50 } 51 52 return 0; 53 } 54 // Result: 55 /* 56 终端解释器 57 非终端解释器 58 终端解释器 59 终端解释器 60 */
Good Good Study, Day Day Up.
顺序 选择 循环 总结
标签:style blog http color os io ar for art
原文地址:http://www.cnblogs.com/Braveliu/p/3956992.html