• <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 閱讀(933) 評論(2)  編輯 收藏 引用
            久久久久人妻一区精品果冻| 人妻精品久久无码专区精东影业| 久久久久亚洲AV片无码下载蜜桃| 日本久久久久亚洲中字幕| 久久99亚洲网美利坚合众国| 久久久一本精品99久久精品66| 99久久无色码中文字幕| 久久精品成人欧美大片| 国内精品久久久久影院薰衣草| 激情伊人五月天久久综合| 久久人妻少妇嫩草AV蜜桃| 久久精品aⅴ无码中文字字幕不卡| 久久精品成人国产午夜| 久久精品国产99国产精品导航 | 久久久91精品国产一区二区三区| 91精品无码久久久久久五月天 | 久久久久人妻精品一区三寸蜜桃| 99精品国产综合久久久久五月天| 91久久精品国产免费直播| 久久婷婷国产剧情内射白浆 | 久久99国产精一区二区三区| 亚洲精品久久久www| 日本三级久久网| 久久久久亚洲AV成人片| 久久热这里只有精品在线观看| 亚洲国产成人久久精品动漫| 久久久精品人妻一区二区三区蜜桃 | 久久av免费天堂小草播放| 久久精品99久久香蕉国产色戒| 国内精品久久久久影院薰衣草| 午夜精品久久久内射近拍高清| 94久久国产乱子伦精品免费| 国产成人综合久久综合| 国产精品国色综合久久| 亚洲国产精品无码久久一区二区| 狠狠色丁香久久婷婷综合蜜芽五月| 精品无码久久久久久久动漫| 国産精品久久久久久久| 久久久久无码精品国产app| 久久久这里有精品中文字幕| 亚洲?V乱码久久精品蜜桃|