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

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            設計模式之command模型

            Command(命令):選擇操作模型,大體上我們可以理解此類設計模型是個函數對象,將函數封裝為對象,以參數傳遞給其他對象或函數,以告訴它們在履行過程中執行特定的操作。
            class Command {
            public:
              virtual void execute() = 0;
            };

            class Hello : public Command {
            public:
              void execute() { cout << "Hello "; }
            };

            class World : public Command {
            public:
              void execute() { cout << "World! "; }
            };

            class IAm : public Command {
            public:
              void execute() { cout << "I'm the command pattern!"; }
            };

            // An object that holds commands:
            class Macro {
              vector<Command*> commands;
            public:
              void add(Command* c) { commands.push_back(c); }
              void run() {
                vector<Command*>::iterator it = commands.begin();
                while(it != commands.end())
                  (*it++)->execute();
              }
            };

            int main() {
              Macro macro;
              macro.add(new Hello);
              macro.add(new World);
              macro.add(new IAm);
              macro.run();
            } ///


            GoF認為command是替代callback的面向對象的替代物,command常用的一個例子就是“撤銷(undo)”操作;一個典型的command模式僅僅創建它并把它傳遞給一個函數或對象。是下面的示例代碼如何消除正常操作和事件處理的decouling(耦合),在事件驅動編程中。
            #include <iostream>
            #include <vector>
            #include <string>
            #include <ctime>
            #include <cstdlib>
            using namespace std;

            // Framework for running tasks:
            class Task {
            public:
              virtual void operation() = 0;
            };

            class TaskRunner {
              static vector<Task*> tasks;
              TaskRunner() {} // Make it a Singleton
              TaskRunner& operator=(TaskRunner&); // Disallowed
              TaskRunner(const TaskRunner&); // Disallowed
              static TaskRunner tr;
            public:
              static void add(Task& t) { tasks.push_back(&t); }
              static void run() {
                vector<Task*>::iterator it = tasks.begin();
                while(it != tasks.end())
                  (*it++)->operation();
              }
            };

            TaskRunner TaskRunner::tr;
            vector<Task*> TaskRunner::tasks;

            class EventSimulator {
              clock_t creation;
              clock_t delay;
            public:
              EventSimulator() : creation(clock()) {
                delay = CLOCKS_PER_SEC/4 * (rand() % 20 + 1);
                cout << "delay = " << delay << endl;
              }
              bool fired() {
                return clock() > creation + delay;
              }
            };

            // Something that can produce asynchronous events:
            class Button {
              bool pressed;
              string id;
              EventSimulator e; // For demonstration
            public:
              Button(string name) : pressed(false), id(name) {}
              void press() { pressed = true; }
              bool isPressed() {
                if(e.fired()) press(); // Simulate the event
                return pressed;
              }
              friend ostream&
              operator<<(ostream& os, const Button& b) {
                return os << b.id;
              }
            };

            // The Command object
            class CheckButton : public Task {
              Button& button;
              bool handled;
            public:
              CheckButton(Button & b) : button(b), handled(false) {}
              void operation() {
                if(button.isPressed() && !handled) {
                  cout << button << " pressed" << endl;
                  handled = true;
                }
              }
            };

            // The procedures that perform the main processing. These
            // need to be occasionally "interrupted" in order to
            // check the state of the buttons or other events:
            void procedure1() {
              // Perform procedure1 operations here.
              // ...
              TaskRunner::run(); // Check all events
            }

            void procedure2() {
              // Perform procedure2 operations here.
              // ...
              TaskRunner::run(); // Check all events
            }

            void procedure3() {
              // Perform procedure3 operations here.
              // ...
              TaskRunner::run(); // Check all events
            }

            int main() {
              srand(time(0)); // Randomize
              Button b1("Button 1"), b2("Button 2"), b3("Button 3");
              CheckButton cb1(b1), cb2(b2), cb3(b3);
              TaskRunner::add(cb1);
              TaskRunner::add(cb2);
              TaskRunner::add(cb3);
              cout << "Control-C to exit" << endl;
              while(true) {
                procedure1();
                procedure2();
                procedure3();
              }
            }
            說明:這里command對象由Task表示,EventSimoulator創建一個隨機延遲事件,它的對象在Button中使用,用來模擬某個不可預知的時間段用戶事件發生的動作。CheckButton是Task的實現,我們也可以把Taskrunner::run()放到一個多線程處理的“計時器”對象中,由此創建一個線程的方案。

            posted on 2009-04-12 08:24 Benjamin 閱讀(448) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久国产精品一区二区| 麻豆精品久久久一区二区| 日韩久久无码免费毛片软件| 精品无码久久久久久久动漫| 久久久精品国产免大香伊 | 亚洲一级Av无码毛片久久精品| 久久这里都是精品| 久久精品蜜芽亚洲国产AV| 久久本道综合久久伊人| 婷婷久久香蕉五月综合加勒比| 九九精品99久久久香蕉| 久久久久av无码免费网| 久久精品国产91久久综合麻豆自制| 久久久久18| 久久免费精品一区二区| 色天使久久综合网天天| 99久久国产亚洲高清观看2024| 亚洲精品无码久久久久去q| 久久国产香蕉一区精品| 国产精品99精品久久免费| 久久精品国产99久久久古代| 久久婷婷人人澡人人| 国产亚洲美女精品久久久久狼| 中文字幕无码免费久久| 亚洲人AV永久一区二区三区久久 | 免费无码国产欧美久久18| 色偷偷888欧美精品久久久| 久久久久亚洲AV无码网站| 久久99九九国产免费看小说| 久久se精品一区精品二区国产| 久久777国产线看观看精品| 青青草原精品99久久精品66| 欧美性大战久久久久久| 狠狠综合久久综合中文88| 久久免费国产精品一区二区| 久久国产精品99精品国产987| 精品国产一区二区三区久久| 99久久无码一区人妻a黑| 精品久久无码中文字幕| 精品一区二区久久| 久久精品18|