【1】什么是命令模式? 命令模式: 【2】命令模式的代码示例: 代码示例:
#if 0 #include <iostream> #include <string> using namespace std; /* * 运算基类 */ class Operation { public: double numberA; double numberB; public: virtual double getResult() { return 0; } }; /* * 加法运算子类 */ class addOperation : public Operation { double getResult() { return numberA + numberB; } }; /* * 减法运算子类 */ class subOperation : public Operation { double getResult() { return numberA - numberB; } }; /* * 乘法运算子类 */ class mulOperation : public Operation { double getResult() { return numberA * numberB; } }; /* * 除法运算子类 */ class divOperation : public Operation { double getResult() { return numberA / numberB; } }; /* * 简单构建工厂 */ class operFactory { public: static Operation *createOperation(char c) { switch (c) { case '+': return new addOperation; break; case '-': return new subOperation; break; case '*': return new mulOperation; break; case '/': return new divOperation; break; default: break; } } }; /* * 客户端应用示例 */ void main() { Operation *oper = operFactory::createOperation('+'); oper->numberA = 9; oper->numberB = 99; cout << oper->getResult() << endl; } /* * 简单工厂模式应用示例 */ #include <iostream> #include <cmath> #include <string> using namespace std; class CashSuper { public: virtual double acceptMoney(double money) = 0; }; class CashNormal : public CashSuper { public: double acceptMoney(double money) { return money; } }; class CashRebate : public CashSuper { private: double discount; public: CashRebate(double dis) { discount = dis; } double acceptMoney(double money) { return money * discount; } }; class CashReturn : public CashSuper { private: double moneyCondition; double moneyReturn; public: CashReturn(double mc, double mr) { moneyCondition = mc; moneyReturn = mr; } double acceptMoney(double money) { double result = money; if (money >= moneyCondition) { result = money - floor(money / moneyCondition) * moneyReturn; } return result; } }; class CashFactory { public: static CashSuper *createCashAccept(string str) { CashSuper *cs = NULL; if ("正常收费" == str) { return new CashNormal(); } else if ("打9折" == str) { return new CashRebate(0.9); } else if ("满300返200" == str) { return new CashReturn(300, 200); } return cs; } }; void main() { CashSuper *cs = NULL; cs = CashFactory::createCashAccept("打9折"); cout << cs->acceptMoney(1000) << endl; cs = CashFactory::createCashAccept("正常收费"); cout << cs->acceptMoney(1000) << endl; } /* * 策略模式 */ #include <iostream> #include <cmath> #include <string> using namespace std; class CashSuper { public: virtual double acceptMoney(double money) = 0; }; class CashNormal : public CashSuper { public: double acceptMoney(double money) { return money; } }; class CashRebate : public CashSuper { private: double discount; public: CashRebate(double dis) { discount = dis; } double acceptMoney(double money) { return money * discount; } }; class CashReturn : public CashSuper { private: double moneyCondition; double moneyReturn; public: CashReturn(double mc, double mr) { moneyCondition = mc; moneyReturn = mr; } double acceptMoney(double money) { double result = money; if (money >= moneyCondition) { result = money - floor(money / moneyCondition) * moneyReturn; } return result; } }; class CashContext { private: CashSuper *cs; public: CashContext(CashSuper *cs) { this->cs = cs; } double getResult(double money) { return cs->acceptMoney(money); } }; void main() { CashSuper *cs; CashContext *cc; double money = 1000; cs = new CashRebate(0.8); cc = new CashContext(cs); cout << cc->getResult(money) << endl; money = 1000; cs = new CashNormal(); cc = new CashContext(cs); cout << cc->getResult(money) << endl; } /* * 策略与工厂模式 */ #include <iostream> #include <cmath> #include <string> using namespace std; class CashSuper { public: virtual double acceptMoney(double money) = 0; }; class CashNormal : public CashSuper { public: double acceptMoney(double money) { return money; } }; class CashRebate : public CashSuper { private: double discount; public: CashRebate(double dis) { discount = dis; } double acceptMoney(double money) { return money * discount; } }; class CashReturn : public CashSuper { private: double moneyCondition; double moneyReturn; public: CashReturn(double mc, double mr) { moneyCondition = mc; moneyReturn = mr; } double acceptMoney(double money) { double result = money; if (money >= moneyCondition) { result = money - floor(money / moneyCondition) * moneyReturn; } return result; } }; class CashContext { private: CashSuper *cs; public: CashContext(string str) { if ("正常收费" == str) { cs = new CashNormal(); } else if ("打9折" == str) { cs = new CashRebate(0.9); } else if ("满300送200" == str) { cs = new CashReturn(300, 200); } } double getResult(double money) { return cs->acceptMoney(money); } }; int main() { double money = 1000; CashContext *cc = new CashContext("打9折"); cout << cc->getResult(money); return 0; } #include <string> #include <iostream> using namespace std; class Person { private: string m_strName; public: Person(string strName) { m_strName = strName; } Person(){} virtual void show() { cout << "装扮的是:" << m_strName << endl; } }; class Finery : public Person { protected: Person *m_component; public: void decorate(Person* component) { m_component = component; } virtual void show() { m_component->show(); } }; class TShirts : public Finery { public: virtual void show() { m_component->show(); cout << "T shirts" << endl; } }; class BigTrouser : public Finery { public: virtual void show() { m_component->show(); cout << "Big Trouser" << endl; } }; int main() { Person *p = new Person("小李"); BigTrouser *bt = new BigTrouser(); TShirts *ts = new TShirts(); bt->decorate(p); ts->decorate(bt); ts->show(); return 0; } #include <iostream> #include <string> using namespace std; class SchoolGirl { public: string name; }; /* * 接口 */ class IGiveGift { public: virtual void giveDolls() = 0; virtual void giveFlowers() = 0; }; /* * 委托类 */ class Pursuit : public IGiveGift { private: SchoolGirl mm; public: Pursuit(SchoolGirl m) { mm = m; } void giveDolls() { cout << mm.name << " 送你娃娃" << endl; } void giveFlowers() { cout << mm.name << " 送你鲜花" << endl; } }; /* * 代理类 */ class Proxy : public IGiveGift { private: Pursuit gg; public: Proxy(SchoolGirl mm) : gg(mm) { } void giveDolls() { gg.giveDolls(); } void giveFlowers() { gg.giveFlowers(); } }; /* * 客户端 */ int main() { SchoolGirl lijiaojiao; lijiaojiao.name = "李娇娇"; Pursuit zhuojiayi(lijiaojiao); Proxy daili(lijiaojiao); daili.giveDolls(); daili.giveFlowers(); return 0; } #include <iostream> #include <string> using namespace std; class Operation { public: double numberA; double numberB; virtual double getResult() { return 0; } }; class addOperation : public Operation { double getResult() { return numberA + numberB; } }; class subOperation : public Operation { double getResult() { return numberA - numberB; } }; class mulOperation : public Operation { double getResult() { return numberA * numberB; } }; class divOperation : public Operation { double getResult() { return numberA / numberB; } }; class IFactory { public: virtual Operation *createOperation() = 0; }; class AddFactory : public IFactory { public: static Operation *createOperation() { return new addOperation(); } }; class SubFactory : public IFactory { public: static Operation *createOperation() { return new subOperation(); } }; class MulFactory : public IFactory { public: static Operation *createOperation() { return new mulOperation(); } }; class DivFactory : public IFactory { public: static Operation *createOperation() { return new divOperation(); } }; int main() { Operation *oper = MulFactory::createOperation(); oper->numberA = 9; oper->numberB = 99; cout << oper->getResult() << endl; return 0; } #include <iostream> #include <string> using namespace std; class Prototype { private: string str; public: Prototype(string s) { str = s; } Prototype() { str = ""; } void show() { cout << str << endl; } virtual Prototype *clone() = 0; }; class ConcretePrototype1 : public Prototype { public: ConcretePrototype1(string s) : Prototype(s) {} ConcretePrototype1(){} virtual Prototype *clone() { ConcretePrototype1 *p = new ConcretePrototype1(); *p = *this; return p; } }; class ConcretePrototype2 : public Prototype { public: ConcretePrototype2(string s) : Prototype(s) {} ConcretePrototype2(){} virtual Prototype *clone() { ConcretePrototype2 *p = new ConcretePrototype2(); *p = *this; return p; } }; int main() { ConcretePrototype1 *test = new ConcretePrototype1("小李"); ConcretePrototype2 *test2 = (ConcretePrototype2 *)test->clone(); test->show(); test2->show(); return 0; } #include <iostream> #include <string> using namespace std; class Resume { private: string name, sex, age, timeArea, company; public: Resume(string s) { name = s; } void setPersonalInfo(string s, string a) { sex = s; age = a; } void setWorkExperience(string t, string c) { timeArea = t; company = c; } void display() { cout << name << " " << sex << " " << age << endl; cout << "工作经历: " << timeArea << " " << company << endl; } Resume *clone() { Resume *b = new Resume(name); b->setPersonalInfo(sex, age); b->setWorkExperience(timeArea, company); return b; } }; int main() { Resume *r = new Resume("李俊宏"); r->setPersonalInfo("男","26"); r->setWorkExperience("2007-2010","读研究生"); r->display(); Resume *r2 = r->clone(); r2->setWorkExperience("2003-2007","读本科"); r->display(); r2->display(); return 0; } #include <iostream> #include <string> using namespace std; class TestPaper { public: void question1() { cout << "1+1=" << answer1() << endl; } void question2() { cout << "1*1=" << answer2() << endl; } virtual string answer1() { return ""; } virtual string answer2() { return ""; } virtual ~TestPaper() { } }; class TestPaperA : public TestPaper { public: string answer1() { return "2"; } virtual string answer2() { return "1"; } }; class TestPaperB : public TestPaper { public: string answer1() { return "3"; } virtual string answer2() { return "4"; } }; int main() { cout << "A的试卷:" << endl; TestPaper *s1 = new TestPaperA(); s1->question1(); s1->question2(); delete s1; cout << endl; cout << "B的试卷:" << endl; TestPaper *s2 = new TestPaperB(); s2->question1(); s2->question2(); return 0; } #include<iostream> #include <vector> #include <string> using namespace std; class AbstractClass { public: void Show() { cout << "我是" << GetName() << endl; } protected: virtual string GetName() = 0; }; class Naruto : public AbstractClass { protected: virtual string GetName() { return "火影史上最帅的六代目---一鸣惊人naruto"; } }; class OnePice : public AbstractClass { protected: virtual string GetName() { return "我是无恶不做的大海贼---路飞"; } }; //客户端 int main() { Naruto* man = new Naruto(); man->Show(); OnePice* man2 = new OnePice(); man2->Show(); return 0; } #include <iostream> #include <string> using namespace std; class Sub1 { public: void f1() { cout << "子系统的方法 1" << endl; } }; class Sub2 { public: void f2() { cout << "子系统的方法 2" << endl; } }; class Sub3 { public: void f3() { cout << "子系统的方法 3" << endl; } }; class Facade { private: Sub1 *s1; Sub2 *s2; Sub3 *s3; public: Facade() { s1 = new Sub1(); s2 = new Sub2(); s3 = new Sub3(); } void method() { s1->f1(); s2->f2(); s3->f3(); } ~Facade() { if (s1) delete s1; if (s2) delete s2; if (s3) delete s3; } }; int main() { Facade *f = new Facade(); f->method(); return 0; } #include <string> #include <iostream> #include <vector> using namespace std; class Person { public: virtual void createHead() = 0; virtual void createHand() = 0; virtual void createBody() = 0; virtual void createFoot() = 0; }; class ThinPerson : public Person { void createHead() { cout << "thin head" << endl; } void createHand() { cout << "thin hand" << endl; } void createBody() { cout << "thin body" << endl; } void createFoot() { cout << "thin foot" << endl; } }; class FatPerson : public Person { void createHead() { cout << "fat head" << endl; } void createHand() { cout << "fat hand" << endl; } void createBody() { cout << "fat body" << endl; } void createFoot() { cout << "fat foot" << endl; } }; class Director { private: Person *p; public: Director(Person *temp) { p = temp; } void create() { p->createHead(); p->createHand(); p->createBody(); p->createFoot(); } }; //客户端代码: int main() { Person *p = new FatPerson(); Person *b = new ThinPerson(); Director *d = new Director(p); Director *s = new Director(b); d->create(); s->create(); delete d; delete p; delete s; delete b; return 0; } #include <string> #include <iostream> #include <vector> using namespace std; class Product { private: vector<string> product; public: void add(string str) { product.push_back(str); } void show() { vector<string>::iterator iter = product.begin(); while (iter != product.end()) { cout << *iter << " "; ++iter; } cout << endl; } }; class Builder { public: virtual void builderA() = 0; virtual void builderB() = 0; virtual Product *getResult() = 0; }; class ConcreteBuilder1 : public Builder { private: Product *product; public: ConcreteBuilder1() { product = new Product(); } virtual void builderA() { product->add("one"); } virtual void builderB() { product->add("two"); } virtual Product *getResult() { return product; } }; class ConcreteBuilder2 : public Builder { private: Product *product; public: ConcreteBuilder2() { product = new Product(); } virtual void builderA() { product->add("A"); } virtual void builderB() { product->add("B"); } virtual Product *getResult() { return product; } }; class Director { private: Product *p; public: void construct(Builder *bd) { bd->builderA(); bd->builderB(); p = bd->getResult(); } Product *getResult() { return p; } }; int main() { Director *director = new Director(); Builder *bd1 = new ConcreteBuilder1(); director->construct(bd1); Product *pbd1 = director->getResult(); pbd1->show(); return 0; } // 观察者模式 #include <iostream> #include <string> #include <list> using namespace std; class Observer; class Subject { protected: list<Observer*> observers; public: string action; public: virtual void attach(Observer*) = 0; virtual void detach(Observer*) = 0; virtual void notify() = 0; }; class Observer { protected: string name; Subject *sub; public: Observer(string name, Subject *sub) { this->name = name; this->sub = sub; } string getName() { return name; } virtual void update() = 0; }; class StockObserver : public Observer { public: StockObserver(string name, Subject *sub) : Observer(name, sub) { } void update(); }; void StockObserver::update() { cout << name << " 收到消息:" << sub->action << endl; if (sub->action == "梁所长来了!") { cout << "我马上关闭股票,装做很认真工作的样子!" << endl; } } class NBAObserver : public Observer { public: NBAObserver(string name, Subject *sub) : Observer(name, sub) { } void update(); }; void NBAObserver::update() { cout << name << " 收到消息:" << sub->action << endl; if (sub->action == "梁所长来了!") { cout << "我马上关闭NBA,装做很认真工作的样子!" << endl; } } class Secretary : public Subject { void attach(Observer *observer) { observers.push_back(observer); } void detach(Observer *observer) { list<Observer *>::iterator iter = observers.begin(); while (iter != observers.end()) { if ((*iter) == observer) { cout << "erase:" << observer->getName() << endl; observers.erase(iter++); } else { ++iter; } } } void notify() { list<Observer *>::iterator iter = observers.begin(); while (iter != observers.end()) { (*iter)->update(); ++iter; } } }; int main() { Subject *dwq = new Secretary(); Observer *xs = new NBAObserver("xiaoshuai", dwq); Observer *zy = new NBAObserver("zouyue", dwq); Observer *lm = new StockObserver("limin", dwq); dwq->attach(xs); dwq->attach(zy); dwq->attach(lm); dwq->action = "去吃饭了!"; dwq->notify(); cout << endl; dwq->action = "梁所长来了!"; dwq->notify(); dwq->detach(lm); dwq->detach(zy); dwq->detach(xs); return 0; } #include <iostream> #include <string> using namespace std; class IUser { public: virtual void getUser() = 0; virtual void setUser() = 0; }; class SqlUser : public IUser { public: void getUser() { cout << "在sql中返回user" << endl; } void setUser() { cout << "在sql中设置user" << endl; } }; class AccessUser : public IUser { public: void getUser() { cout << "在Access中返回user" << endl; } void setUser() { cout << "在Access中设置user" << endl; } }; class IDepartment { public: virtual void getDepartment() = 0; virtual void setDepartment() = 0; }; class SqlDepartment : public IDepartment { public: void getDepartment() { cout << "在sql中返回Department" << endl; } void setDepartment() { cout << "在sql中设置Department" << endl; } }; class AccessDepartment : public IDepartment { public: void getDepartment() { cout << "在Access中返回Department" << endl; } void setDepartment() { cout << "在Access中设置Department" << endl; } }; class IFactory { public: virtual IUser *createUser() = 0; virtual IDepartment *createDepartment() = 0; }; class SqlFactory : public IFactory { public: IUser *createUser() { return new SqlUser(); } IDepartment *createDepartment() { return new SqlDepartment(); } }; class AccessFactory : public IFactory { public: IUser *createUser() { return new AccessUser(); } IDepartment *createDepartment() { return new AccessDepartment(); } }; /*************************************************************/ class DataAccess { private: static string db; public: static IUser *createUser() { if (db == "access") { return new AccessUser(); } else if (db == "sql") { return new SqlUser(); } } static IDepartment *createDepartment() { if (db == "access") { return new AccessDepartment(); } else if (db == "sql") { return new SqlDepartment(); } } }; string DataAccess::db = "sql"; /*************************************************************/ int main() { IFactory *factory; IUser *user; IDepartment *department; factory = new AccessFactory(); user = factory->createUser(); department = factory->createDepartment(); user->getUser(); user->setUser(); department->getDepartment(); department->setDepartment(); user = DataAccess::createUser(); department = DataAccess::createDepartment(); user->getUser(); user->setUser(); department->getDepartment(); department->setDepartment(); return 0; } #include <iostream> #include <string> using namespace std; class Work; class State; class ForenonnState; class State { public: virtual void writeProgram(Work*) = 0; }; class Work { public: int hour; State *current; Work(); void writeProgram() { current->writeProgram(this); } }; class EveningState : public State { public: void writeProgram(Work *w) { cout << "当前时间: " << w->hour << "心情很好,在看《明朝那些事儿》,收获很大!" << endl; } }; class AfternoonState : public State { public: void writeProgram(Work *w) { if (w->hour < 19) { cout << "当前时间: " << w->hour << "下午午睡后,工作还是精神百倍!" << endl; } else { w->current = new EveningState(); w->writeProgram(); } } }; class ForenonnState : public State { public: void writeProgram(Work *w) { if (w->hour < 12) { cout << "当前时间: " << w->hour << "上午工作精神百倍!" << endl; } else { w->current = new AfternoonState(); w->writeProgram(); } } }; Work::Work() { current = new ForenonnState(); } int main() { Work *w = new Work(); w->hour = 21; w->writeProgram(); return 0; } //Reuslt: //当前时间: 21心情很好,在看《明朝那些事儿》,收获很大! #include <iostream> #include <string> using namespace std; class Adaptee { public: virtual void myRequest() { cout << "实际上的接口" << endl; } }; class Target { public: virtual void request() = 0; virtual ~Target(){} }; class Adapter : public Target { private: Adaptee adaptee; public: void request() { adaptee.myRequest(); } }; int main() { Target *target = new Adapter(); target->request(); delete target; return 0; } //Result: //实际上的接口 #include <iostream> #include <string> using namespace std; class Player { public: string name; Player(string name) { this->name = name; } virtual void attack() = 0; virtual void defence() = 0; }; class Forwards : public Player { public: Forwards(string name) : Player(name){} void attack() { cout << name << "前锋进攻" << endl; } void defence() { cout << name << "前锋防守" << endl; } }; class Center : public Player { public: Center(string name) : Player(name){} void attack() { cout << name << "中锋进攻" << endl; } void defence() { cout << name << "中锋防守" << endl; } }; class Backwards : public Player { public: Backwards(string name) : Player(name){} void attack() { cout << name << "后卫进攻" << endl; } void defence() { cout << name << "后卫防守" << endl; } }; /*****************************************************************/ class ForeignCenter { public: string name; ForeignCenter(string name) { this->name = name; } void myAttack() { cout << name << "外籍中锋进攻" << endl; } void myDefence() { cout << name << "外籍中锋防守" << endl; } }; /*****************************************************************/ class Translator : public Player { private: ForeignCenter *fc; public: Translator(string name) : Player(name) { fc = new ForeignCenter(name); } void attack() { fc->myAttack(); } void defence() { fc->myDefence(); } }; /*****************************************************************/ int main() { Player *p1 = new Center("李俊宏"); p1->attack(); p1->defence(); Translator *tl = new Translator("姚明"); tl->attack(); tl->defence(); return 0; } //Result: /* 李俊宏中锋进攻 李俊宏中锋防守 姚明外籍中锋进攻 姚明外籍中锋防守 */ #include <iostream> #include <string> #include <vector> using namespace std; class Memo { public: string state; Memo(string state) { this->state = state; } }; class Originator { public: string state; void setMemo(Memo *memo) { state = memo->state; } Memo *createMemo() { return new Memo(state); } void show() { cout << state << endl; } }; class Caretaker { public: vector<Memo *> memo; public: void save(Memo *memo) { (this->memo).push_back(memo); } Memo *getState(int i) { return memo[i]; } }; int main() { Originator *og = new Originator(); Caretaker *ct = new Caretaker(); og->state = "on"; og->show(); ct->save(og->createMemo()); og->state = "off"; og->show(); ct->save(og->createMemo()); og->state = "middle"; og->show(); ct->save(og->createMemo()); og->setMemo( ct->getState(1) ); og->show(); return 0; } //Result: /* on off middle off */ #include <iostream> #include <vector> #include <string> using namespace std; class Component { public: string name; Component(string name) { this->name = name; } virtual void add(Component *) = 0; virtual void remove(Component *) = 0; virtual void display(int) = 0; }; class Leaf : public Component { public: Leaf(string name) : Component(name) {} void add(Component *c) { cout << "leaf cannot add" << endl; } void remove(Component *c) { cout << "leaf cannot remove" << endl; } void display(int depth) { string str(depth, '-'); str += name; cout << str << endl; } }; class Composite : public Component { private: vector<Component*> component; public: Composite(string name) : Component(name) {} void add(Component *c) { component.push_back(c); } void remove(Component *c) { vector<Component*>::iterator iter = component.begin(); while (iter != component.end()) { if (*iter == c) { component.erase(iter++); } else { iter++; } } } void display(int depth) { string str(depth, '-'); str += name; cout << str << endl; vector<Component*>::iterator iter=component.begin(); while (iter != component.end()) { (*iter)->display(depth + 2); iter++; } } }; int main() { Component *p = new Composite("小李"); p->add(new Leaf("小王")); p->add(new Leaf("小强")); Component *sub = new Composite("小虎"); sub->add(new Leaf("小王")); sub->add(new Leaf("小明")); sub->add(new Leaf("小柳")); p->add(sub); p->display(0); cout << "*******" << endl; sub->display(2); return 0; } //Result: /* 小李 --小王 --小强 --小虎 ----小王 ----小明 ----小柳 ******* --小虎 ----小王 ----小明 ----小柳 */ #include <iostream> #include <string> using namespace std; class Iterator; class Aggregate { public: virtual Iterator *createIterator() = 0; }; class Iterator { public: virtual void first() = 0; virtual void next() = 0; virtual bool isDone() = 0; }; class ConcreteAggregate : public Iterator { public: void first() {} void next() {} bool isDone() {} }; int main() { return 0; } #include <iostream> #include <string> using namespace std; class Singleton { private: int i; static Singleton *instance; Singleton(int i) { this->i = i; } public: static Singleton *getInstance() { return instance; } void show() { cout << i << endl; } }; Singleton* Singleton::instance = new Singleton(8899); class A : public Singleton { }; int main() { Singleton *s = Singleton::getInstance(); Singleton *s2 = A::getInstance(); cout << s << endl; cout << s2 << endl; cout << (s == s2) << endl; return 0; } #include <iostream> #include <string> using namespace std; class HandsetSoft { public: virtual void run() = 0; }; class HandsetGame : public HandsetSoft { public: void run() { cout << "运行手机游戏" << endl; } }; class HandsetAddressList : public HandsetSoft { public: void run() { cout << "运行手机通讯录" << endl; } }; class HandsetBrand { protected: HandsetSoft *soft; public: void setHandsetSoft(HandsetSoft *soft) { this->soft = soft; } virtual void run() = 0; }; class HandsetBrandN : public HandsetBrand { public: void run() { soft->run(); } }; class HandsetBrandM : public HandsetBrand { public: void run() { soft->run(); } }; int main() { HandsetBrand *hb; hb = new HandsetBrandM(); hb->setHandsetSoft(new HandsetGame()); hb->run(); hb->setHandsetSoft(new HandsetAddressList()); hb->run(); return 0; } #endif #include <iostream> #include <string> #include <list> using namespace std; class Barbecuer { public: void bakeMutton() { cout << "烤羊肉串" << endl; } void bakeChickenWing() { cout << "烤鸡翅" << endl; } }; class Command { protected: Barbecuer *receiver; public: Command(Barbecuer *receiver) { this->receiver = receiver; } virtual void executeCommand() = 0; }; class BakeMuttonCommand : public Command { public: BakeMuttonCommand(Barbecuer *receiver) : Command(receiver) {} void executeCommand() { receiver->bakeMutton(); } }; class BakeChikenWingCommand : public Command { public: BakeChikenWingCommand(Barbecuer *receiver) : Command(receiver) {} void executeCommand() { receiver->bakeChickenWing(); } }; class Waiter { private: Command *command; public: void setOrder(Command *command) { this->command = command; } void notify() { command->executeCommand(); } }; class Waiter2 { private: list<Command*> orders; public: void setOrder(Command *command) { orders.push_back(command); } void cancelOrder(Command *command) {} void notify() { list<Command*>::iterator iter = orders.begin(); while (iter != orders.end()) { (*iter)->executeCommand(); iter++; } } }; int main() { Barbecuer *boy = new Barbecuer(); Command *bm1 = new BakeMuttonCommand(boy); Command *bm2 = new BakeMuttonCommand(boy); Command *bc1 = new BakeChikenWingCommand(boy); cout << "Waiter2:" << endl; Waiter2 *girl2 = new Waiter2(); girl2->setOrder(bm1); girl2->setOrder(bc1); girl2->setOrder(bm2); girl2->notify(); cout << "Waiter:" << endl; Waiter *girl = new Waiter(); girl->setOrder(bm1); girl->notify(); girl->setOrder(bm2); girl->notify(); girl->setOrder(bc1); girl->notify(); return 0; } //Result: /* Waiter2: 烤羊肉串 烤鸡翅 烤羊肉串 Waiter: 烤羊肉串 烤羊肉串 烤鸡翅 */
http://www.cnblogs.com/Braveliu/p/3956683.html