• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 183,  comments - 10,  trackbacks - 0

            來自于《大話設計模式》
            解釋器模式(Interpreter):
            給定一個語言,定義它的文法的一種表示,并定義一個解釋器,這個解釋器使用該表示來解釋語言中的句子。

            行為型


            UML 類圖:



            代碼實現 C++:
             1 #include <iostream>
             2 #include <string>
             3 #include <list>
             4 using namespace std;
             5 
             6 class Context
             7 {
             8 private:
             9     string input;
            10     string output;
            11 public:
            12     string getInput()
            13     {
            14         return input;
            15     }
            16     void setInput(const string& s)
            17     {
            18         input = s;
            19     }
            20     string getOutput()
            21     {
            22         return output;
            23     }
            24     void setOutput(const string& s)
            25     {
            26         output = s;
            27     }
            28 };
            29 
            30 class AbstractExpression
            31 {
            32 public:
            33     virtual void Interpret(Context* context) = 0;
            34 };
            35 
            36 class TerminalExpression : public AbstractExpression
            37 {
            38 public:
            39     virtual void Interpret(Context* context)
            40     {
            41         context->setOutput("終端解釋器:" + context->getInput());
            42         cout << context->getOutput() << endl;
            43     }
            44 };
            45 
            46 class NonterminalExpression : public AbstractExpression
            47 {
            48 public:
            49     virtual void Interpret(Context* context)
            50     {
            51         context->setOutput("非終端解釋器:" + context->getInput());
            52         cout << context->getOutput() << endl;
            53     }
            54 };
            55 
            56 int main()
            57 {
            58     Context* context = new Context;
            59     context->setInput("abc");
            60     list<AbstractExpression*> lae;
            61 
            62     lae.push_back(new TerminalExpression);
            63     lae.push_back(new NonterminalExpression);
            64     lae.push_back(new TerminalExpression);
            65     lae.push_back(new TerminalExpression);
            66 
            67     for (list<AbstractExpression*>::iterator iter = lae.begin(); iter != lae.end(); ++iter)
            68     {
            69         (*iter)->Interpret(context);
            70     }
            71 
            72     for (list<AbstractExpression*>::iterator iter = lae.begin(); iter != lae.end(); ++iter)
            73     {
            74         delete (*iter);
            75     }
            76     delete context;
            77 
            78     return 0;
            79 }
            posted on 2011-04-30 14:32 unixfy 閱讀(932) 評論(2)  編輯 收藏 引用
            久久久久免费精品国产| 婷婷久久精品国产| 欧美成人免费观看久久| 久久久久久青草大香综合精品 | 97久久精品人人澡人人爽| 久久AAAA片一区二区| 漂亮人妻被中出中文字幕久久 | 人人狠狠综合久久亚洲| 国产A三级久久精品| 精品久久久久久久久久中文字幕| 久久这里都是精品| 亚洲国产二区三区久久| 久久久久亚洲精品日久生情 | 99久久www免费人成精品 | 99久久人妻无码精品系列| 久久综合视频网站| 国产高潮久久免费观看| 精品久久久久久中文字幕人妻最新 | 久久精品中文无码资源站| 青青草原综合久久大伊人导航 | 亚洲精品美女久久久久99小说| 欧美久久综合性欧美| 天天躁日日躁狠狠久久| 久久久国产99久久国产一| 精品综合久久久久久97超人| 97久久超碰成人精品网站| 国产精品久久久久a影院| 亚洲午夜久久久精品影院| 亚洲精品无码久久一线| 久久这里都是精品| 久久99热这里只频精品6| 三级韩国一区久久二区综合| 国产农村妇女毛片精品久久| 97精品伊人久久大香线蕉app| 久久人人爽爽爽人久久久| 久久综合亚洲欧美成人| 久久久久亚洲精品中文字幕| 精品久久久久久国产三级| 精品免费久久久久国产一区| 91精品国产91热久久久久福利| 久久免费高清视频|