《命令模式》

一、建立命令队列;二、可以将命令记入日志;三、接收请求的一方可以拒绝;四、添加一个新命令类不影响其它类;

命令模式把请求一个操作的对象与知道怎么操行一个操作的对象分开

命令模式感觉就是一个领导接受客户端的命令,让底下的人去做,但是比较特殊。继承命令

 

上图

 

 

上代码

 

[cpp]  view plain copy
  1. // Command.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. //************************************************************************/      
  4. /* @filename    Command.cpp 
  5. @author       wallwind   
  6. @createtime    2012/10/24 00:00 
  7. @function     命令模式 
  8. @email       wochenglin@qq.com   
  9. */      
  10. /************************************************************************/     
  11.   
  12. #include "stdafx.h"  
  13. #include <iostream>  
  14. #include <string>  
  15. #include <vector>  
  16. using namespace std;  
  17.   
  18.   
  19. //烤肉师傅  
  20. class Barbucer  
  21. {  
  22. public:  
  23.     void MakeMutton()  
  24.     {  
  25.         cout<<"烤羊肉"<<endl;  
  26.     }  
  27.     void MakeChickenWing()  
  28.     {  
  29.         cout<<"烤鸡翅膀"<<endl;  
  30.     }  
  31. };  
  32.   
  33. //抽象命令类  
  34. class Command  
  35. {  
  36. protected:  
  37.     Barbucer* receiver;  
  38. public:  
  39.     Command(Barbucer* temp)  
  40.     {  
  41.         receiver = temp;  
  42.     }  
  43.     virtual void ExecuteCmd()=0;  
  44. };  
  45.   
  46. //烤羊肉命令  
  47. class BakeMuttonCmd : public Command  
  48. {  
  49. public:  
  50.     BakeMuttonCmd(Barbucer* temp) : Command(temp){}  
  51.     virtual void ExecuteCmd()  
  52.     {  
  53.         receiver->MakeMutton();  
  54.     }  
  55. };  
  56.   
  57.   
  58. //烤鸡翅  
  59. class ChickenWingCmd : public Command  
  60. {  
  61. public:  
  62.     ChickenWingCmd(Barbucer* temp) : Command(temp){}  
  63.   
  64.     virtual void ExecuteCmd()  
  65.     {  
  66.         receiver->MakeChickenWing();  
  67.     }  
  68. };  
  69.   
  70. //服务员类  
  71. class Waiter  
  72. {  
  73. protected:  
  74.     vector<Command*> m_commandList;  
  75. public:  
  76.     void SetCmd(Command* temp)  
  77.     {  
  78.         m_commandList.push_back(temp);  
  79.         cout<<"增加定单"<<endl;  
  80.     }  
  81.   
  82.     //通知执行  
  83.     void Notify()  
  84.     {  
  85.         vector<Command*>::iterator p=m_commandList.begin();  
  86.         while(p!=m_commandList.end())  
  87.         {  
  88.             (*p)->ExecuteCmd();  
  89.             p++;  
  90.         }  
  91.     }  
  92. };  
  93.   
  94.   
  95.   
  96.   
  97.   
  98. int _tmain(int argc, _TCHAR* argv[])  
  99. {  
  100.   
  101.     //店里添加烤肉师傅、菜单、服务员等顾客  
  102.     Barbucer* barbucer=new Barbucer();  
  103.     Command* cmd= new BakeMuttonCmd(barbucer);  
  104.     Command* cmd2=new ChickenWingCmd(barbucer);  
  105.     Waiter* girl = new Waiter();  
  106.     //点菜  
  107.     girl->SetCmd(cmd);  
  108.     girl->SetCmd(cmd2);  
  109.     //服务员通知  
  110.     girl->Notify();  
  111.     return 0;  
  112.   
  113.     return 0;  
  114. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值