命令模式(Command)将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
测试用例:
int main(){
Barbecuer boy;
BakeChickenWingCommand bakechickenwingcommand1(boy);
BakeChickenWingCommand bakechickenwingcommand2(boy);
BakeMuttonCommand bakemuttoncommand1(boy);
BakeMuttonCommand bakemuttoncommand2(boy);
Waiter girl;
girl.SetOrder(&bakechickenwingcommand1);
girl.SetOrder(&bakechickenwingcommand2);
girl.SetOrder(&bakemuttoncommand1);
girl.SetOrder(&bakemuttoncommand2);
girl.Notify();
girl.CancelOrder(&bakechickenwingcommand2);
girl.Notify();
return 0;
}
类实现:
class Barbecuer{
public:
void BakeMutton()
{
cout << "Meat\n";
}
void BakeChickenWing()
{
cout << "Chicken\n";
}
};
class Command{
protected:
Barbecuer receiver;
public:
Command(){}
Command(Barbecuer & b) :receiver(b){}
virtual void ExcuteCommand() = 0;
};
class BakeMuttonCommand :public Command{
public:
BakeMuttonCommand(Barbecuer & b) { receiver = b; }
void ExcuteCommand(){ receiver.BakeMutton(); }
};
class BakeChickenWingCommand :public Command{
public:
BakeChickenWingCommand(Barbecuer & b) { receiver = b; }
void ExcuteCommand(){ receiver.BakeChickenWing(); }
};
class Waiter{
list<Command *>orders;
public:
void SetOrder(Command * comptr);
void CancelOrder(Command * comptr);
void Notify();
};
void Waiter::SetOrder(Command * comptr){
orders.push_back(comptr);
cout << "Add order\n";
}
void Waiter::CancelOrder(Command * comptr){
orders.remove(comptr);
cout << "Cancel order\n";
}
void Waiter::Notify(){
for each (Command * var in orders){
var->ExcuteCommand();
}
}
总结:
- 较容易的设计一个命令队列;
- 在需要的情况下,可以较容易地将命令记入日志;
- 允许接收请求的一方决定是否否决请求;
- 可以容易地实现对请求的撤销和重做;
- 由于加进新的命令类不影响其他的类,因为增加新的具体命令类很容易。
- 命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分割开。