Command Pattern

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

2.Command 模式结构图

3.实现

 1 #ifndef _RECEIVER_H_ 
 2 #define _RECEIVER_H_
 3 
 4 class Reciever 
 5 { 
 6 public: 
 7     Reciever();
 8     ~Reciever();
 9     void Action();
10 protected:
11 private:
12 };
13 
14 #endif
Receiver.h
 1 #include "Receiver.h"
 2 #include <iostream>
 3 
 4 Reciever::Reciever() 
 5 {
 6 
 7 }
 8 Reciever::~Reciever()
 9 {
10 
11 }
12 void Reciever::Action() 
13 {
14     std::cout<<"Reciever action......."<<std::endl;
15 }
Receiver.cpp
 1 #ifndef _COMMAND_H_ 
 2 #define _COMMAND_H_
 3 
 4 class Reciever;
 5 
 6 class Command 
 7 { 
 8 public: 
 9     virtual ~Command();
10     virtual void Excute() = 0;
11 protected: 
12     Command();
13 private:
14 };
15 
16 class ConcreteCommand:public Command 
17 { 
18 public: 
19     ConcreteCommand(Reciever* rev);
20     ~ConcreteCommand();
21     void Excute();
22 protected:
23 private: 
24     Reciever* _rev; 
25 };
26 
27 #endif
Command.h
 1 #include "Command.h"
 2 #include "Receiver.h"
 3 #include <iostream>
 4 
 5 Command::Command() 
 6 {
 7 
 8 }
 9 Command::~Command() 
10 {
11 
12 }
13 void Command::Excute()
14 {
15 
16 }
17 ConcreteCommand::ConcreteCommand(Reciever* rev) 
18 { 
19     this->_rev = rev;
20 }
21 ConcreteCommand::~ConcreteCommand() 
22 { 
23     delete this->_rev; 
24 }
25 void ConcreteCommand::Excute() 
26 { 
27     _rev->Action();
28     std::cout<<"ConcreteCommand..."<<std::endl;
29 }
Command.cpp
 1 #ifndef _INVOKER_H_ 
 2 #define _INVOKER_H_
 3 
 4 class Command;
 5 
 6 class Invoker 
 7 { 
 8 public: 
 9     Invoker(Command* cmd);
10     ~Invoker();
11     void Invoke();
12 protected:
13 private: 
14     Command* _cmd;
15 };
16 
17 #endif
Invoker.h
 1 #include "Invoker.h"
 2 #include "Command.h"
 3 #include <iostream>
 4 
 5 Invoker::Invoker(Command* cmd) 
 6 { 
 7     _cmd = cmd;
 8 }
 9 Invoker::~Invoker() 
10 { 
11     delete _cmd;
12 }
13 void Invoker::Invoke() 
14 { 
15     _cmd->Excute(); 
16 }
Invoker.cpp
 1 #include "Command.h" 
 2 #include "Invoker.h" 
 3 #include "Receiver.h"
 4 #include <iostream> 
 5 
 6 using namespace std;
 7 
 8 int main(int argc,char* argv[]) 
 9 { 
10     Reciever* rev = new Reciever();
11     Command* cmd = new ConcreteCommand(rev);
12     Invoker* inv = new Invoker(cmd);
13     inv->Invoke();
14     return 0; 
15 }
main.cpp

4.将请求接收者的处理抽象出来作为参数传给Command对象,实际也就是回调的机制(Callback)来实现这一点,也就是说将处理操作方法地址(在对象内部)通过参数传递给Command对象(涉及到C++成员函数指针的概念)。简单示例:

 1 #ifndef _RECEIVER_H_ 
 2 #define _RECEIVER_H_
 3 
 4 class Reciever 
 5 { 
 6 public: 
 7     Reciever();
 8     ~Reciever();
 9     void Action();
10 protected:
11 private:
12 };
13 
14 #endif
Receiver.h
 1 #include "Receiver.h"
 2 #include <iostream>
 3 
 4 Reciever::Reciever() 
 5 {
 6 
 7 }
 8 Reciever::~Reciever()
 9 {
10 
11 }
12 void Reciever::Action() 
13 {
14     std::cout<<"Reciever action......."<<std::endl;
15 }
Receiver.cpp
 1 #ifndef _COMMAND_H_ 
 2 #define _COMMAND_H_
 3 
 4 class Command 
 5 { 
 6 public: 
 7     virtual ~Command(){}
 8     virtual void Excute() = 0;
 9 protected:
10     Command(){}
11 private:
12 };
13 
14 template<class Reciever> 
15 class SimpleCommand:public Command
16 { 
17 public: 
18     typedef void (Reciever::* Action)();
19 
20     SimpleCommand(Reciever* rev,Action act) 
21     {
22         _rev = rev;
23         _act = act; 
24     }
25 
26     virtual void Excute() 
27     { 
28         (_rev->* _act)();
29     }
30 
31     ~SimpleCommand() 
32     { 
33         delete _rev;
34     }
35 protected:
36 private: 
37     Reciever* _rev;
38     Action _act; 
39 };
40 
41 #endif
Command.h
 1 #include "Command.h"
 2 #include "Receiver.h"
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main(int arc,char* argv[]) 
 7 { 
 8     Reciever* rev = new Reciever();
 9     Command* cmd = new SimpleCommand<Reciever>(rev,&Reciever::Action);
10     cmd->Excute();
11     return 0; 
12 }
main.cpp

posted on 2015-07-23 17:40  那个人好像一条狗  阅读(183)  评论(0编辑  收藏  举报