• <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 閱讀(934) 評論(2)  編輯 收藏 引用
            久久人与动人物a级毛片| 久久久噜噜噜久久中文福利| 中文字幕成人精品久久不卡| 久久中文字幕视频、最近更新 | 色婷婷久久综合中文久久一本| AA级片免费看视频久久| 久久久国产亚洲精品| 久久免费视频观看| 亚洲国产另类久久久精品| 99久久亚洲综合精品成人| 久久天天躁夜夜躁狠狠躁2022| 美女写真久久影院| 久久久亚洲欧洲日产国码二区| 久久综合一区二区无码| 精品久久777| 国内精品伊人久久久久av一坑| 色99久久久久高潮综合影院| 久久99国产精品久久| 亚洲午夜久久久久久久久久 | 精品久久久噜噜噜久久久 | 久久国产成人亚洲精品影院| 久久精品国产亚洲77777| 久久久午夜精品| 青青青青久久精品国产h久久精品五福影院1421 | 亚洲狠狠久久综合一区77777 | 久久精品国产久精国产一老狼| 国产精品无码久久久久| 精品国产福利久久久| 久久不射电影网| 一级做a爱片久久毛片| 国产一级持黄大片99久久| 国产成人久久激情91| 久久精品国产99国产精偷 | 一本久久综合亚洲鲁鲁五月天| 久久这里只有精品久久| 天天综合久久久网| 亚洲国产精品久久久久网站| 久久se精品一区精品二区| 94久久国产乱子伦精品免费| 久久久免费观成人影院| 久久这里只有精品首页|