行为型 - 命令模式(Command)

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

概念

Command 模式典型的结构图为:

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

备注:这里的Invoke 消息就是下面Notify()来处理的。

代码实现

模型代码实现

#ifndef _RECIEVER_H_
#define _RECIEVER_H_
class Reciever
{
public:
	Reciever();
	~Reciever();
	void Action();

protected:
private:
};
#endif //~_RECIEVER_H_
1
2
3
4
5
6
7
8
9
10
11
12
#include "Reciever.h"
#include <iostream>

Reciever::Reciever()
{
}

Reciever::~Reciever()
{
}

void Reciever::Action()
{
    std::cout << "Reciever action......." << std::endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef _COMMAND_H_
#define _COMMAND_H_
#include <iostream>

using namespace std;
class Reciever;

class Command
{
public:
    virtual ~Command();
    virtual void Excute() = 0;

protected:
    Command();

private:
};

class ConcreteCommandA : public Command
{
public:
    ConcreteCommandA(Reciever *rev);
    ~ConcreteCommandA();
    void Excute();

protected:
private:
    Reciever *_rev;
};

class ConcreteCommandB : public Command
{
public:
    ConcreteCommandB(Reciever *rev);
    ~ConcreteCommandB();
    void Excute();

protected:
private:
    Reciever *_rev;
};

#endif //~_COMMAND_H_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Command.h"
#include "Reciever.h"

Command::Command()
{
}

Command::~Command()
{
}

void Command::Excute()
{
}

ConcreteCommandA::ConcreteCommandA(Reciever *rev)
{
    this->_rev = rev;
}

ConcreteCommandA::~ConcreteCommandA()
{
}

void ConcreteCommandA::Excute()
{
    cout << "ConcreteCommandA Excute" << endl;
    _rev->Action();
}

ConcreteCommandB::ConcreteCommandB(Reciever *rev)
{
    this->_rev = rev;
}

ConcreteCommandB::~ConcreteCommandB()
{
}

void ConcreteCommandB::Excute()
{
    cout << "ConcreteCommandB Excute" << endl;
    _rev->Action();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef _INVOKER_H_
#define _INVOKER_H_
#include <vector>
#include <iostream>

using namespace std;

class Command;
class Invoker
{
public:
    Invoker(Command *cmd);
    Invoker();
    ~Invoker();

    void Add(Command *cmd);
    void Remove(Command *cmd);
    void RemoveAll();
    void Notify();

protected:
private:
    Command *_cmd;
    vector<Command *> _commands;
};
#endif //~_INVOKER_H_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "Invoke.h"
#include "Command.h"

Invoker::Invoker(Command *cmd) : _cmd(cmd)
{
}

Invoker::Invoker()
{
}

Invoker::~Invoker()
{
    delete _cmd;
}

void Invoker::Add(Command *cmd)
{
    _commands.push_back(cmd);
}

void Invoker::Remove(Command *cmd)
{
    for (std::vector<Command *>::iterator iter = _commands.begin(); iter != _commands.end(); )
    {
        if (*iter == cmd)
        {
            iter =_commands.erase(iter);
        }
        else
        {
            iter++;
        }
    }
}

void Invoker::RemoveAll()
{
    _commands.clear();
}

void Invoker::Notify()
{
    std::vector<Command *>::iterator it = _commands.begin();
    for (; it != _commands.end(); ++it)
    {
        _cmd = *it;
        _cmd->Excute();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "Reciever.h"
#include "Invoke.h"
#include "Command.h"

int main(int argc, char *argv[])
{
	Reciever *pstReceiver = new Reciever();
	Command *pstConcreteCommandA = new ConcreteCommandA(pstReceiver);
	Command *pstConcreteCommandB = new ConcreteCommandB(pstReceiver);
	Invoker *pstInvoke = new Invoker();

	pstInvoke->Add(pstConcreteCommandA);
	pstInvoke->Add(pstConcreteCommandA);
	pstInvoke->Add(pstConcreteCommandB);
	pstInvoke->Notify();
	cout << "------------------" << endl;

	pstInvoke->Remove(pstConcreteCommandA); //撤销操作
	// pstInvoke->Remove(pstConcreteCommandB);
	pstInvoke->Notify();
	cout << "------------------" << endl;

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

上述例子中

void Add(Command *cmd);//添加命令
void Remove(Command *cmd);//删除命令
void RemoveAll();//删除全部命令
void Notify();//通知执行所有命令
1
2
3
4
[root@VM-16-6-centos Command]# ./CommandTest
ConcreteCommandA Excute
Reciever action.......
ConcreteCommandA Excute
Reciever action.......
ConcreteCommandB Excute
Reciever action.......
------------------
ConcreteCommandB Excute
Reciever action.......
------------------

1
2
3
4
5
6
7
8
9
10
11
12