设计模式 - Command 模式(命令模式)
作用:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
UML结构图:
解析:
Comnand模式的思想是把命令封装在一个类中,就是这里的Command基类,同时把接收对象也封装在一个类中就是这里的Receiver类中,由调用这个命令的类也就是这里的Invoker类来调用。其实,如果弄清楚了Command模式的原理,就会发现其实它和注册回调函数的原理是很相似的,而在面向过程的设计中的回调函数其实和这里的Command类的作用是一致的,采用Command模式解耦了命令的发出者和命令的执行者。
代码实现:
Command.h
1 #ifndef COMMAND_H
2 #define COMMAND_H
3
4 class Command
5 {
6 public:
7 virtual ~Command() {}
8
9 virtual void Execute() = 0;
10 };
11
12 class Receiver
13 {
14 public:
15 void Action();
16 };
17
18 class Invoker
19 {
20 public:
21 Invoker(Command *pCommand);
22 ~Invoker();
23
24 void Invoke();
25 private:
26 Command *m_pCommand;
27 };
28
29 class ConcreateComand
30 : public Command
31 {
32 public:
33 ConcreateComand(Receiver* pReceiver);
34 virtual ~ConcreateComand();
35
36 virtual void Execute();
37
38 private:
39 Receiver* m_pReceiver;
40 };
41
42 #endif
43
2 #define COMMAND_H
3
4 class Command
5 {
6 public:
7 virtual ~Command() {}
8
9 virtual void Execute() = 0;
10 };
11
12 class Receiver
13 {
14 public:
15 void Action();
16 };
17
18 class Invoker
19 {
20 public:
21 Invoker(Command *pCommand);
22 ~Invoker();
23
24 void Invoke();
25 private:
26 Command *m_pCommand;
27 };
28
29 class ConcreateComand
30 : public Command
31 {
32 public:
33 ConcreateComand(Receiver* pReceiver);
34 virtual ~ConcreateComand();
35
36 virtual void Execute();
37
38 private:
39 Receiver* m_pReceiver;
40 };
41
42 #endif
43
Command.cpp
1 #include "Command.h"
2 #include <iostream>
3
4 void Receiver::Action()
5 {
6 std::cout << "Receiver Action\n";
7 }
8
9 Invoker::Invoker(Command *pCommand)
10 : m_pCommand(pCommand)
11 {
12
13 }
14
15 Invoker::~Invoker()
16 {
17 delete m_pCommand;
18 m_pCommand = NULL;
19 }
20
21 void Invoker::Invoke()
22 {
23 if (NULL != m_pCommand)
24 {
25 m_pCommand->Execute();
26 }
27 }
28
29 ConcreateComand::ConcreateComand(Receiver* pReceiver)
30 : m_pReceiver(pReceiver)
31 {
32
33 }
34
35 ConcreateComand::~ConcreateComand()
36 {
37 delete m_pReceiver;
38 m_pReceiver = NULL;
39 }
40
41 void ConcreateComand::Execute()
42 {
43 if (NULL != m_pReceiver)
44 {
45 m_pReceiver->Action();
46 }
47
48 std::cout << "Execute by ConcreateComand\n";
49 }
50
2 #include <iostream>
3
4 void Receiver::Action()
5 {
6 std::cout << "Receiver Action\n";
7 }
8
9 Invoker::Invoker(Command *pCommand)
10 : m_pCommand(pCommand)
11 {
12
13 }
14
15 Invoker::~Invoker()
16 {
17 delete m_pCommand;
18 m_pCommand = NULL;
19 }
20
21 void Invoker::Invoke()
22 {
23 if (NULL != m_pCommand)
24 {
25 m_pCommand->Execute();
26 }
27 }
28
29 ConcreateComand::ConcreateComand(Receiver* pReceiver)
30 : m_pReceiver(pReceiver)
31 {
32
33 }
34
35 ConcreateComand::~ConcreateComand()
36 {
37 delete m_pReceiver;
38 m_pReceiver = NULL;
39 }
40
41 void ConcreateComand::Execute()
42 {
43 if (NULL != m_pReceiver)
44 {
45 m_pReceiver->Action();
46 }
47
48 std::cout << "Execute by ConcreateComand\n";
49 }
50
Main.cpp
1 #include "Command.h"
2 #include <stdlib.h>
3
4 int main()
5 {
6 Receiver* pReceiver = new Receiver();
7 Command* pCommand = new ConcreateComand(pReceiver);
8 Invoker* pInvoker = new Invoker(pCommand);
9
10 pInvoker->Invoke();
11
12 delete pInvoker;
13
14 system("pause");
15
16 return 0;
17 }
18
2 #include <stdlib.h>
3
4 int main()
5 {
6 Receiver* pReceiver = new Receiver();
7 Command* pCommand = new ConcreateComand(pReceiver);
8 Invoker* pInvoker = new Invoker(pCommand);
9
10 pInvoker->Invoke();
11
12 delete pInvoker;
13
14 system("pause");
15
16 return 0;
17 }
18