• <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| 亚洲国产一成久久精品国产成人综合| 日本久久久久久中文字幕| 久久精品免费观看| 51久久夜色精品国产| 91精品国产高清久久久久久国产嫩草| 国产精品久久自在自线观看| 国内精品久久九九国产精品| 精品久久777| 久久精品成人| 99久久精品免费看国产一区二区三区 | 久久精品视频一| 亚洲国产另类久久久精品| 久久午夜羞羞影院免费观看| 东京热TOKYO综合久久精品| 久久免费高清视频| 久久久久国产一区二区三区| 2020久久精品亚洲热综合一本| 伊人久久综合精品无码AV专区| 久久国产精品成人片免费| 伊人久久免费视频| 精品国产乱码久久久久久人妻| 国产精品视频久久| 狠狠色丁香久久婷婷综合蜜芽五月| 人妻精品久久久久中文字幕69| 精品国产青草久久久久福利| 国内精品综合久久久40p| 国产精品成人久久久久久久| 国产成人无码精品久久久性色| 91久久精品电影| 色狠狠久久AV五月综合| 美女久久久久久| 99久久免费国产特黄|