设计模式之解释器模式(Interpreter)
来源:互联网 发布:php中美元符号 编辑:程序博客网 时间:2024/05/18 00:14
如果一个特定类型的问题发生频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的例子。这样就可以创建一个解释器,该解释器通过解释这些例子来解决该问题。
代码
- // Interpert.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <string>
- using namespace std;
- // 解释器模式:给定一种语言,定义它的文法的一种表示,并定义一个解释器
- // 该解释器使用该表示来解释语言中的句子
- // 类似于程序语言或者脚本
- // 假设情景是一个脚本控制系统
- // wasd上下左右方向,正数数字为移动步数
- // 简化一下吧,测试和开发支持wasd字母,移动步数为1-9的正整数
- class Fire
- {
- public:
- };
- class Action
- {
- public:
- virtual void Interpret(string &strCmd)
- {
- if (strCmd.length() == 0)
- {
- return;
- }
- else
- {
- string strNum = strCmd.substr(1, 2);
- m_nNum = atoi(strNum.c_str());
- strCmd = strCmd.substr(2);
- Excute();
- }
- }
- virtual void Excute() = 0;
- protected:
- int m_nNum;
- };
- // W
- class Forward : public Action
- {
- public:
- void Excute()
- {
- for (int i = 0; i < m_nNum; i ++)
- {
- printf("前进 ");
- }
- }
- };
- // A
- class Left : public Action
- {
- public:
- void Excute()
- {
- for (int i = 0; i < m_nNum; i ++)
- {
- printf("左移 ");
- }
- }
- };
- class Right : public Action
- {
- public:
- void Excute()
- {
- for (int i = 0; i < m_nNum; i ++)
- {
- printf("右移 ");
- }
- }
- };
- // S
- class Back : public Action
- {
- public:
- void Excute()
- {
- for (int i = 0; i < m_nNum; i ++)
- {
- printf("后退 ");
- }
- }
- };
- void Interper(string &str)
- {
- string tmpStr = str.substr(0,1);
- Action *pAction = NULL;
- char t;
- memcpy(&t, tmpStr.c_str(), 1);
- switch (t)
- {
- case 'W':
- pAction = new Forward();
- break;
- case 'A':
- pAction = new Left();
- break;
- case 'D':
- pAction = new Right();
- break;
- case 'S':
- pAction = new Back();
- break;
- default:
- break;
- }
- if (pAction)
- {
- pAction->Interpret(str);
- delete pAction;
- pAction = NULL;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- string str = "A7S4D4W1A5D5";
- while(str.length() != 0)
- {
- Interper(str);
- }
- printf("\n");
- return 0;
- }
- 设计模式之Interpreter解释器模式
- 设计模式之解释器模式,interpreter
- 设计模式之解释器模式(interpreter)
- 设计模式之:解释器模式(Interpreter)
- 设计模式之解释器模式(Interpreter)
- 设计模式之(十六)- 解释器模式(interpreter)
- 23种设计模式之解释器模式(Interpreter)
- 设计模式之解释器模式(Interpreter)
- 设计模式之解释器模式(Interpreter)
- 设计模式总结之Interpreter Pattern(解释器模式)
- 【设计模式 - 15】之解释器模式(Interpreter)
- 设计模式之解释器模式(Interpreter)
- 设计模式:解释器模式(Interpreter)
- 设计模式:解释器模式(Interpreter)
- [设计模式]解释器模式(Interpreter)
- 设计模式之Interpreter(解释器)
- 设计模式之Interpreter(解释器)
- 设计模式之Interpreter(解释器)
- why the blog borned? 此 博客为何而生?
- spring依赖注入
- 荷兰王子昏迷18个月去世
- OpenSSL - DES
- Windows下批量重命名
- 设计模式之解释器模式(Interpreter)
- 使用apachetop来分析apache实时日志
- iOS7 Sprite Kit 学习
- 从数据库读取图片输出到页面中
- git下添加新项目及用户
- Android emulator
- 学习数组时要注意的地方
- 数字游戏
- Strut1和Spring的整合