Command模式(C++命令模式)

来源:互联网 发布:智多星水利软件 编辑:程序博客网 时间:2024/05/22 13:31

重新温习GOF23到第19个命令模式了,写下来方便自己理解

用经理,秘书,邮局来理解这个模式还是比较好的。

经理:喂,小丽呀,这儿有封信,请帮我尽快寄出去…
秘书:好的,经理,我马上去办!
秘书拿着这封信,到一家邮局将信寄了出去。邮局最终也将信投递到了收件人手中。

这是一个很常见的生活工作场景,不过这里面:经理、秘书和邮局,三者在不知不觉间,演绎了一出面向对象设计模式:Command模式。Command模式是我打算讲的第一个行为型设计模式。
Command模式的内容大致如此:

将一个请求封装成一个对象(Command对象),并赋予该对象一个执行接口。Command对象在执行命令时,并不一定自己切身做这件事,而是将请求转发给另一个真正做这件事情的对象(Receiver对象),由Receiver对象最终完成请求操作。Command模式使得请求发起者和请求接受者之间解除耦合。

对象来对象去的,比较抽象,想想秘书小丽就好多了:经理需要发起一个请求(发送一封信),他找到了小丽(Command对象),交给她去做这件事。小丽将请求交给邮局(Receiver对象)去最终完成信投递任务。在整个过程中,经理认为执行其命令的是小丽,他不关心也不用知道是哪家邮局最终投递的这封信,即使是小丽自己跑腿儿将信交给收件人,经理也不用关心。

 

命令模式的有点:

1.能够容易地设计一个命令队列;

2.在需要的情况下,可以比较容易地将命令记入日志。

3.可以容易的实现对请求的撤销和重做。

4.由于加进新的具体命令类不影响其他的类,因此增加新的具体命令类很容易。

 

 

写了个小小的Demo,代码如下:

Command.h

//////////////////////////////////////////////////////////////////////////// author: Jeson Yang// File:Command.h// date:2014.11.6//////////////////////////////////////////////////////////////////////////#include <iostream>#include <vector>using namespace std;class Reciever{public:void Action(){cout << "Do action !!" <<endl;}};class Icommand{public:virtual ~Icommand() {}virtual void Excute() = 0;protected:Icommand() {}};class Read_Command:public Icommand{public:Read_Command(Reciever *rev):m_rev(rev){}virtual void Excute() {cout << "Read Command.." << endl;m_rev->Action();}~Read_Command(){}private:Reciever *m_rev;};class Write_Command:public Icommand{public:Write_Command(Reciever *rev):m_rev(rev){}virtual void Excute() {cout << "Read Command.." << endl;m_rev->Action();}~Write_Command(){}private:Reciever *m_rev;};class Invoker{public:Invoker(Icommand* cmd):m_cmd(cmd){}Invoker(){}~Invoker(){delete m_cmd;}void Notify(){std::vector<Icommand*>::iterator it = cmdList.begin();for(it;it != cmdList.end();++it){m_cmd = *it;m_cmd->Excute();}}void AddCmd(Icommand* pcmd){cmdList.push_back(pcmd);}void DelCmd(Icommand* pcmd){for (std::vector<Icommand*>::iterator iter = cmdList.begin(); iter != cmdList.end(); iter++){if (*iter == pcmd){cmdList.erase(iter);}}}private:Icommand* m_cmd;std::vector<Icommand*> cmdList;};


 

 

 


 

main.cpp

//////////////////////////////////////////////////////////////////////////// author: Jeson Yang// File:main.cpp// date:2014.11.6//////////////////////////////////////////////////////////////////////////#include <iostream>#include <vector>#include "command.h"using namespace std;int main(){Reciever* rev = new Reciever();Icommand* cmd1 = new Read_Command(rev);Icommand* cmd2 = new Write_Command(rev);Invoker inv;inv.AddCmd(cmd1);inv.AddCmd(cmd2);inv.Notify();system("pause");return 0;}


 


08上测试通过。

 

1 0
原创粉丝点击