• <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

            來自于《大話設(shè)計(jì)模式》
            解釋器模式(Interpreter):
            給定一個(gè)語(yǔ)言,定義它的文法的一種表示,并定義一個(gè)解釋器,這個(gè)解釋器使用該表示來解釋語(yǔ)言中的句子。

            行為型


            UML 類圖:



            代碼實(shí)現(xiàn) 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 閱讀(937) 評(píng)論(2)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久亚洲日韩精品一区二区三区| 亚洲午夜久久影院| 久久精品国产99国产精品导航| 人人妻久久人人澡人人爽人人精品| 亚洲中文字幕久久精品无码喷水| 97精品伊人久久大香线蕉app| 欧美777精品久久久久网| 久久精品中文字幕大胸| 狼狼综合久久久久综合网| 国产—久久香蕉国产线看观看| 香港aa三级久久三级老师2021国产三级精品三级在 | 综合久久精品色| 国产成人精品久久亚洲高清不卡 | 7国产欧美日韩综合天堂中文久久久久| 久久久这里只有精品加勒比| 久久综合狠狠色综合伊人| 超级碰碰碰碰97久久久久| 色偷偷888欧美精品久久久| 亚洲熟妇无码另类久久久| 久久久久久久久66精品片| 99久久精品免费国产大片| 亚洲αv久久久噜噜噜噜噜| 亚洲国产成人久久精品99| 国产精品狼人久久久久影院| 精品久久久久香蕉网| 2021国产精品午夜久久| 久久天天躁狠狠躁夜夜av浪潮| 久久国产成人精品麻豆| 国产精品一区二区久久不卡| 久久久国产精华液| 精品综合久久久久久97| 久久精品免费大片国产大片| 国产成人精品综合久久久久| 久久久久九国产精品| 亚洲精品高清国产一久久| 777米奇久久最新地址| 国产精品久久久久久久久软件| 国内精品免费久久影院| 久久露脸国产精品| 亚洲精品NV久久久久久久久久| 美女久久久久久|