• <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)  編輯 收藏 引用
            999久久久免费精品国产| 久久免费线看线看| 久久中文字幕无码专区| 精品久久久无码中文字幕天天| 久久久久久毛片免费看| 香蕉久久影院| 国产精品久久久久久一区二区三区 | 狠狠色丁香婷婷久久综合五月| 国产精品亚洲综合久久| 99久久超碰中文字幕伊人| 国产视频久久| 久久亚洲私人国产精品vA | 国产精品久久影院| 久久国产成人午夜aⅴ影院| 久久精品人人做人人爽电影| 久久久91精品国产一区二区三区| 日本加勒比久久精品| 久久亚洲欧美日本精品| 亚洲精品无码久久久久| 老司机午夜网站国内精品久久久久久久久 | 国产精品综合久久第一页| 午夜精品久久久久久毛片| 久久久久人妻一区精品| 久久亚洲国产午夜精品理论片| 国产69精品久久久久久人妻精品| 99热热久久这里只有精品68| 99久久这里只有精品| 狠狠色婷婷久久综合频道日韩| 久久久久一级精品亚洲国产成人综合AV区| 亚洲αv久久久噜噜噜噜噜| 国产精品久久久香蕉| 一级做a爰片久久毛片毛片| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久国产一区二区| 女人香蕉久久**毛片精品| 国产精品久久国产精品99盘| 精品熟女少妇av免费久久| 久久国产精品77777| 97久久香蕉国产线看观看| 国产精品久久久久9999| 欧美伊香蕉久久综合类网站|