现在的位置: 首页 > 综合 > 正文

餐馆那些事之:Command Pattern

2017年11月03日 ⁄ 综合 ⁄ 共 4091字 ⁄ 字号 评论关闭
分类: Design Pattern 1493人阅读 评论(1) 收藏 举报
1. 概述
Composite Pattern是一种非常优雅的模式,实际使用中出现频率相当高。其通过对请求进行封装,解除了请求的发布者和具体实现者之间的耦合关系。
类图:

Customer:命令的发布者
Command:命令的接口
Concreate_command:具体命令的实现
Worker:命令的具体执行者

2. 实例
一个顾客跑到餐馆吃饭,他告诉服务员需要点的菜,服务员整理顾客的需要,然后告诉厨师,厨师负责具体做菜
对应上面的类图
Customer:顾客
Concrete_command:服务员
Worker:厨师
顾客和厨师解耦,顾客不需要关心具体哪个厨师在给他做菜,这就是command模式的好处之一。
实现代码如下:

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. //厨师,具体的worker  
  5. class Cooker  
  6. {  
  7. public:  
  8.         void Do()  
  9.         {  
  10.                 cout << "cooker cook" << endl;  
  11.         };  
  12. };  
  13.   
  14. //command接口  
  15. class Command  
  16. {  
  17. public:  
  18.         virtual ~Command()  
  19.         {  
  20.         }  
  21.   
  22.         virtual void Do() = 0;  
  23. };  
  24.   
  25. //concrete_command  
  26. class Waiter: public Command  
  27. {  
  28. public:  
  29.         Waiter(Cooker* cooker)  
  30.         {  
  31.                 _cooker = cooker;  
  32.         }  
  33.   
  34.         virtual void Do()  
  35.         {  
  36.                 _cooker->Do();  
  37.         }  
  38.   
  39. private:  
  40.         //命令的执行者  
  41.         Cooker* _cooker;  
  42. };  
  43.   
  44. //命令的发起者  
  45. class Customer  
  46. {  
  47. public:  
  48.         Customer(Command* command)  
  49.         {  
  50.                 _command = command;  
  51.         }  
  52.   
  53.         void order()  
  54.         {  
  55.                 cout << "customer order" << endl;  
  56.                 _command->Do();  
  57.         }  
  58. private:  
  59.   
  60.         Command* _command;  
  61. };  
  62.   
  63. int main()  
  64. {  
  65.         //命令的执行者  
  66.         Cooker cooker;  
  67.   
  68.         //命令的封装  
  69.         Command* command = new Waiter(&cooker);  
  70.   
  71.         //命令的发起者  
  72.         Customer customer(command);  
  73.   
  74.         //执行命令  
  75.         customer.order();  
  76.   
  77.         if(command)  
  78.         {  
  79.                 delete command;  
  80.         }  
  81.   
  82.         return 0;  
  83. }  

输出:
$ ./bin/test       
customer order
cooker cook

3. undo
do操作完成了客户的需要,而undo操作则需要表示客户“后悔了”。undo操作使用非常多,常用的软件里面基本上都有undo的实现,使用undo,只要在command命令增加undo的接口,同时,concrete_command需要保存do之前的状态。
提供给客户取消订单的操作。
代码:

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. class Cooker  
  6. {  
  7. public:  
  8.         Cooker()  
  9.         {  
  10.                 _value = 0;  
  11.         }  
  12.   
  13.         void Do()  
  14.         {  
  15.                 _value++;  
  16.                 cout << "cooker do"  <<  endl;  
  17.         };  
  18.           
  19.         //标记值  
  20.         void value()  
  21.         {  
  22.                 cout << "value=" << _value <<  endl;  
  23.         };  
  24.   
  25. private:  
  26.         int _value;  
  27. };  
  28.   
  29. class Command  
  30. {  
  31. public:  
  32.         virtual ~Command()  
  33.         {  
  34.         }  
  35.   
  36.         virtual void Do() = 0;  
  37.         virtual void unDo() = 0;  
  38. };  
  39.   
  40.   
  41. class Waiter: public Command  
  42. {  
  43. public:  
  44.         Waiter(Cooker* cooker)  
  45.         {  
  46.                 _cooker = cooker;  
  47.                 _bak_cooker = new Cooker;  
  48.         }  
  49.   
  50.         ~Waiter()  
  51.         {  
  52.                 if(_bak_cooker)  
  53.                 {  
  54.                         delete _bak_cooker;  
  55.                         _bak_cooker = NULL;  
  56.                 }  
  57.         }  
  58.   
  59.         virtual void Do()  
  60.         {  
  61.                 cout << "waiter do" << endl;  
  62.                 //保留状态  
  63.                 *_bak_cooker = *_cooker;  
  64.                 _cooker->Do();  
  65.         }  
  66.   
  67.         //取消操作  
  68.         virtual void unDo()  
  69.         {  
  70.                 cout << "waiter undo" << endl;  
  71.                 //恢复状态  
  72.                 *_cooker = *_bak_cooker;  
  73.         }  
  74.   
  75. private:  
  76.         Cooker* _cooker;  
  77.         Cooker* _bak_cooker;  
  78. };  
  79.   
  80. class Customer  
  81. {  
  82. public:  
  83.         Customer(Command* command)  
  84.         {  
  85.                 _command = command;  
  86.         }  
  87.   
  88.         void order()  
  89.         {  
  90.                 cout << "customer order" << endl;  
  91.                 _command->Do();  
  92.         }  
  93.   
  94.         void unDo()  
  95.         {  
  96.                 cout << "customer cancel rder" << endl;  
  97.                 _command->unDo();  
  98.         }  
  99. private:  
  100.   
  101.         Command* _command;  
  102. };  
  103.   
  104. int main()  
  105. {  
  106.         Cooker cooker;  
  107.         Command* command = new Waiter(&cooker);  
  108.         Customer customer(command);  
  109.   
  110.         customer.order();  
  111.         cooker.value();  
  112.   
  113.         customer.unDo();  
  114.         cooker.value();  
  115.   
  116.         if(command)  
  117.         {  
  118.                 delete command;  
  119.         }  
  120.   
  121.         return 0;  
  122. }  

输出:
$ ./bin/test 
customer order
waiter do
cooker do
value=1
customer cancel rder
waiter undo
value=0

抱歉!评论已关闭.