设计模式之解释器模式(Interpreter)

来源:互联网 发布:php中美元符号 编辑:程序博客网 时间:2024/05/18 00:14

      如果一个特定类型的问题发生频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的例子。这样就可以创建一个解释器,该解释器通过解释这些例子来解决该问题。




代码


[cpp] view plaincopy
  1. // Interpert.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <string>  
  6. using namespace std;  
  7.   
  8. // 解释器模式:给定一种语言,定义它的文法的一种表示,并定义一个解释器  
  9. // 该解释器使用该表示来解释语言中的句子  
  10.   
  11. // 类似于程序语言或者脚本  
  12.   
  13. // 假设情景是一个脚本控制系统  
  14. // wasd上下左右方向,正数数字为移动步数  
  15. // 简化一下吧,测试和开发支持wasd字母,移动步数为1-9的正整数  
  16.   
  17.   
  18. class Fire  
  19. {  
  20. public:  
  21.       
  22.   
  23. };  
  24.   
  25. class Action  
  26. {  
  27. public:  
  28.     virtual void Interpret(string &strCmd)  
  29.     {  
  30.         if (strCmd.length() == 0)  
  31.         {  
  32.             return;  
  33.         }  
  34.         else  
  35.         {  
  36.             string strNum       = strCmd.substr(1, 2);  
  37.             m_nNum = atoi(strNum.c_str());  
  38.             strCmd = strCmd.substr(2);  
  39.             Excute();  
  40.         }  
  41.     }  
  42.     virtual void Excute() = 0;  
  43. protected:  
  44.     int     m_nNum;  
  45.   
  46. };  
  47.   
  48.   
  49. // W  
  50. class Forward : public Action  
  51. {  
  52. public:  
  53.     void Excute()  
  54.     {  
  55.         for (int i = 0; i < m_nNum; i ++)  
  56.         {  
  57.             printf("前进  ");  
  58.         }  
  59.     }  
  60. };  
  61.   
  62. // A  
  63. class Left : public Action  
  64. {  
  65. public:  
  66.     void Excute()  
  67.     {  
  68.         for (int i = 0; i < m_nNum; i ++)  
  69.         {  
  70.             printf("左移  ");  
  71.         }  
  72.     }  
  73. };  
  74.   
  75. class Right : public Action  
  76. {  
  77. public:  
  78.     void Excute()  
  79.     {  
  80.         for (int i = 0; i < m_nNum; i ++)  
  81.         {  
  82.             printf("右移  ");  
  83.         }  
  84.     }  
  85. };  
  86.   
  87. // S  
  88. class Back : public Action  
  89. {  
  90. public:  
  91.     void Excute()  
  92.     {  
  93.         for (int i = 0; i < m_nNum; i ++)  
  94.         {  
  95.             printf("后退  ");  
  96.         }  
  97.     }  
  98. };  
  99.   
  100. void Interper(string &str)  
  101. {  
  102.     string tmpStr = str.substr(0,1);  
  103.     Action  *pAction = NULL;  
  104.   
  105.     char t;  
  106.     memcpy(&t, tmpStr.c_str(), 1);  
  107.     switch (t)  
  108.     {  
  109.     case 'W':  
  110.         pAction = new Forward();  
  111.         break;  
  112.     case 'A':  
  113.         pAction = new Left();  
  114.         break;  
  115.     case 'D':  
  116.         pAction = new Right();  
  117.         break;  
  118.     case 'S':  
  119.         pAction = new Back();  
  120.         break;  
  121.     default:  
  122.         break;  
  123.     }  
  124.     if (pAction)  
  125.     {  
  126.         pAction->Interpret(str);  
  127.         delete pAction;  
  128.         pAction = NULL;  
  129.     }  
  130. }  
  131.   
  132. int _tmain(int argc, _TCHAR* argv[])  
  133. {  
  134.     string str = "A7S4D4W1A5D5";  
  135.     while(str.length() != 0)  
  136.     {  
  137.         Interper(str);  
  138.     }  
  139.     printf("\n");  
  140.   
  141.     return 0;  
  142. }