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

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            設(shè)計模式之command模型

            Command(命令):選擇操作模型,大體上我們可以理解此類設(shè)計模型是個函數(shù)對象,將函數(shù)封裝為對象,以參數(shù)傳遞給其他對象或函數(shù),以告訴它們在履行過程中執(zhí)行特定的操作。
            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認(rèn)為command是替代callback的面向?qū)ο蟮奶娲?command常用的一個例子就是“撤銷(undo)”操作;一個典型的command模式僅僅創(chuàng)建它并把它傳遞給一個函數(shù)或?qū)ο蟆J窍旅娴氖纠a如何消除正常操作和事件處理的decouling(耦合),在事件驅(qū)動編程中。
            #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創(chuàng)建一個隨機(jī)延遲事件,它的對象在Button中使用,用來模擬某個不可預(yù)知的時間段用戶事件發(fā)生的動作。CheckButton是Task的實(shí)現(xiàn),我們也可以把Taskrunner::run()放到一個多線程處理的“計時器”對象中,由此創(chuàng)建一個線程的方案。

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

            久久国产高潮流白浆免费观看| 久久久久久青草大香综合精品| 蜜臀av性久久久久蜜臀aⅴ| 久久精品国产亚洲AV大全| 99久久夜色精品国产网站| 亚洲&#228;v永久无码精品天堂久久| 久久亚洲精品国产精品婷婷| 午夜久久久久久禁播电影| 99久久精品久久久久久清纯| 久久精品国产清自在天天线| 欧美亚洲另类久久综合| 伊人色综合久久天天人守人婷 | 伊人热热久久原色播放www| 97久久超碰国产精品旧版| 一本色综合久久| 97久久精品人人做人人爽| 久久久国产乱子伦精品作者| 亚洲精品午夜国产va久久| 嫩草影院久久99| 91精品国产综合久久久久久| 一本色道久久88精品综合| 久久久久免费视频| 老司机国内精品久久久久| 久久人人爽爽爽人久久久| 色综合久久天天综线观看| 久久国产美女免费观看精品| 2021久久精品国产99国产精品| 久久人人爽人人爽人人片av麻烦| 久久国产香蕉一区精品| 激情久久久久久久久久| 四虎国产精品免费久久5151| 国产精品99精品久久免费| 欧美精品久久久久久久自慰| 亚洲精品美女久久久久99| 精品久久久无码21p发布| 偷窥少妇久久久久久久久| 国产精品久久久久久久app| 久久久久久综合网天天| 久久99热这里只有精品66| 亚洲精品乱码久久久久66| 青青草原精品99久久精品66|