Command 模式

Command 模式通过将请求封装到一个对象(Command)中,并将请求的接受者存放具体的 ConcreteCommand 类中(Receiver)中,从而实现调用操作的对象和操作的具体实现

者之间的解耦。

Command 模式结构图中,将请求的接收者(处理者)放到 Command 的具体子类
ConcreteCommand 中,当请求到来时(Invoker 发出 Invoke 消息激活 Command 对象) ,
ConcreteCommand 将处理请求交给 Receiver对象进行处理。

 1 ////////////Command.h//////////////////////////////////////////////////////////////
 2 #pragma once
 3 class Reciever;
 4 class Command
 5 {
 6 public:
 7     virtual ~Command();
 8     virtual void Excute() = 0 ;
 9 protected:
10     Command();
11 private:
12 };
13 
14 class ConcreteCommand1 : public Command
15 {
16 public:
17     ~ConcreteCommand1();
18     ConcreteCommand1(Reciever* rec);
19     void Excute() ; 
20 protected:
21 private:
22     Reciever* _rec ;
23 };
24 
25 class ConcreteCommand2 : public Command
26 {
27 public:
28     ~ConcreteCommand2();
29     ConcreteCommand2(Reciever* rec);
30     void Excute() ; 
31 protected:
32 private:
33     Reciever* _rec ;
34 };
 1 /////////Command.cpp/////////////////////////////////////////////////////////////////
 2 #include "Command.h"
 3 #include "Reciever.h"
 4 Command::Command()
 5 {
 6 
 7 }
 8 Command::~Command()
 9 {
10 
11 }
12 
13 ConcreteCommand1::ConcreteCommand1(Reciever* rec)
14 {
15     this->_rec = rec ;
16 }
17 void ConcreteCommand1::Excute()
18 {
19     this->_rec->Action1();
20 }
21 
22 ConcreteCommand1::~ConcreteCommand1()
23 {
24     delete _rec;
25 }
26 
27 
28 
29 ConcreteCommand2::ConcreteCommand2(Reciever* rec)
30 {
31     this->_rec = rec ;
32 }
33 void ConcreteCommand2::Excute()
34 {
35     this->_rec->Action2();
36 }
37 
38 ConcreteCommand2::~ConcreteCommand2()
39 {
40     delete _rec;
41 }
 1 //////////////Invoker.h////////////////////////////////////////////////////////////
 2 #pragma once
 3 #include <vector>
 4 
 5 class Command;
 6 class Invoker
 7 {
 8 public:
 9     void setcmd(Command* cmd);
10     void Notify();
11 protected:
12 private:
13     std::vector<Command*> _cmds ;
14 };
 1 //////Invoker.cpp////////////////////////////////////////////////////////////////////
 2 #include "Invoker.h"
 3 #include "Command.h"
 4 #include <vector>
 5 #include <iostream>
 6 using namespace std;
 7 void Invoker::setcmd(Command* cmd)
 8 {
 9     this->_cmds.push_back(cmd);
10     cout<<"增加请求"<<endl;
11 }
12 
13 void Invoker::Notify()
14 {
15     vector<Command*>::const_iterator it = _cmds.begin();
16     for (;it != _cmds.end() ; it++)
17     {
18         (*it)->Excute();
19     }
20 }
 1 ////////////Reciever.h//////////////////////////////////////////////////////////////
 2 #pragma once
 3 class Reciever
 4 {
 5 public:
 6     ~Reciever();
 7     Reciever();
 8     void Action1();
 9     void Action2();
10 protected:
11 private:
12 };
 1 /////////Reciever.cpp/////////////////////////////////////////////////////////////////
 2 #include "Reciever.h"
 3 #include <iostream>
 4 using namespace std ;
 5 Reciever::Reciever()
 6 {
 7 
 8 }
 9 Reciever::~Reciever()
10 {
11 
12 }
13 void Reciever::Action1()
14 {
15     cout<<"处理具体的请求1"<<endl;
16 }
17 
18 void Reciever::Action2()
19 {
20     cout<<"处理具体的请求2"<<endl;
21 }
 1 ////////main.cpp//////////////////////////////////////////////////////////////////
 2 #include "Command.h"
 3 #include "Invoker.h"
 4 #include "Reciever.h"
 5 #include <iostream>
 6 using namespace std;
 7 int main()
 8 {
 9     Reciever* rec = new Reciever();//创建处理者
10     Invoker* inv = new Invoker();//创建请求者
11 
12     Command* cmd1 = new ConcreteCommand1(rec);
13     Command* cmd2 = new ConcreteCommand2(rec);
14 
15     //得到请求
16     inv->setcmd(cmd1);
17     inv->setcmd(cmd2);
18 
19     //发出请求
20     inv->Notify();
21 
22     getchar();
23     return 0 ;
24 }

 

 

posted @ 2014-06-05 21:02  月轩  阅读(393)  评论(0编辑  收藏  举报