• <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)  編輯 收藏 引用
            久久精品国产99国产精品| 一级女性全黄久久生活片免费 | 日本欧美久久久久免费播放网| 成人综合久久精品色婷婷| 亚洲va久久久噜噜噜久久狠狠| 久久久久无码精品国产| 日韩久久久久中文字幕人妻| 亚洲成色www久久网站夜月| 国产女人aaa级久久久级| 一本色道久久综合狠狠躁 | 综合久久给合久久狠狠狠97色| 国产V亚洲V天堂无码久久久| 久久精品亚洲乱码伦伦中文| 久久综合狠狠综合久久| 日韩久久久久中文字幕人妻| 国产精品久久久久影院嫩草| 久久久久久曰本AV免费免费| 精品久久久无码中文字幕| 国产99精品久久| 国产69精品久久久久9999APGF| 久久综合视频网站| 亚洲午夜久久影院| 91精品国产高清久久久久久io | 国内精品久久久久影院一蜜桃| 亚洲国产精品嫩草影院久久| 久久成人18免费网站| 嫩草影院久久国产精品| 国内精品九九久久久精品| 色欲久久久天天天综合网| 伊人久久大香线蕉精品不卡| 久久青青草原精品国产软件| 久久这里只有精品视频99| 久久久久久亚洲精品无码| 国产免费久久久久久无码| 久久精品人妻一区二区三区| 国产精自产拍久久久久久蜜| 久久久久99精品成人片| 久久无码国产| 久久久久人妻一区二区三区| 亚洲国产精品无码久久一线| 亚洲狠狠婷婷综合久久蜜芽|