• <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 閱讀(449) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久SE精品一区二区| 久久九色综合九色99伊人| 久久综合久久综合亚洲| 色99久久久久高潮综合影院| 伊人久久大香线蕉综合影院首页| 久久久久久毛片免费播放| 久久久99精品成人片中文字幕 | 久久99精品久久只有精品| 久久久久久a亚洲欧洲aⅴ| 亚洲欧美久久久久9999| 久久91综合国产91久久精品 | 久久国产精品77777| 久久精品国产只有精品66| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 国产精品久久永久免费| 三级三级久久三级久久| 国产呻吟久久久久久久92| 久久精品国产精品亚洲毛片| 亚洲欧洲久久av| 久久精品这里只有精99品| 国产精品青草久久久久婷婷| 色综合久久无码中文字幕| 亚洲精品99久久久久中文字幕| 999久久久国产精品| 精品久久久久久久| 99久久精品国产免看国产一区| 久久天天躁狠狠躁夜夜躁2O2O| 国产精品成人久久久| 久久久精品国产Sm最大网站| 情人伊人久久综合亚洲| 成人久久精品一区二区三区| 亚洲va久久久噜噜噜久久男同| 久久无码高潮喷水| 国内精品久久久久影院薰衣草| 伊人久久亚洲综合影院| 久久亚洲精品无码VA大香大香| 综合久久给合久久狠狠狠97色| 久久久久久久精品妇女99| 狠狠精品久久久无码中文字幕| 亚洲va国产va天堂va久久| 久久精品无码专区免费青青|