• <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 閱讀(936) 評論(2)  編輯 收藏 引用
            久久久久18| 伊人色综合久久天天人手人婷| 久久精品国产亚洲av瑜伽| 欧美精品久久久久久久自慰| 中文国产成人精品久久不卡| 久久亚洲精精品中文字幕| 国内精品久久久久久野外| 久久国产免费观看精品3| 日本免费一区二区久久人人澡| 久久精品无码一区二区日韩AV| 日本加勒比久久精品| 国内精品人妻无码久久久影院导航 | 久久精品中文闷骚内射| 日日狠狠久久偷偷色综合免费| 久久久久久国产精品美女| 久久综合噜噜激激的五月天| 亚洲国产精品久久66| 日本WV一本一道久久香蕉| 色综合久久综精品| 国产精品99久久久久久宅男小说| 潮喷大喷水系列无码久久精品| 国产高潮国产高潮久久久91| 少妇熟女久久综合网色欲| 久久精品成人免费观看97| 久久精品国产只有精品2020| 无码国产69精品久久久久网站| 一97日本道伊人久久综合影院| 久久国产精品久久国产精品| 无码人妻精品一区二区三区久久| 亚洲国产精品嫩草影院久久 | 99久久精品免费| 亚洲午夜久久久久久噜噜噜| 亚洲人成网站999久久久综合| 久久精品国产亚洲Aⅴ香蕉 | 久久只有这精品99| 久久91精品国产91久久小草| 亚洲另类欧美综合久久图片区| 久久中文精品无码中文字幕| 漂亮人妻被黑人久久精品| A级毛片无码久久精品免费 | 伊人久久综在合线亚洲2019|