背景
在软件的构建过程中“行为请求中”与“行为实现者”通常呈现出一种“紧耦合”。但在某些场合-比如需要对行为进行“记录、撤销”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,可以实现二者之间的松耦合。
特点
将一个请求(行为)封装为一个对象,从而使你可用不同的请求对客户进行参数化。对请求排队或记录请求日志,以及支持可撤销的操作。
目的
将“行为请求者”与“行为实现者”解耦,在面向对象语言中,常见的手段是“将行为抽象为对象”。
实现方法
实现Command接口的具体命令对象ConcreteCommand,有时候根据需要可能会保存一些额外的状态信息。通过使用Composite模式,可以将多个“命令”封装为一个“复合命令”。
代码实现
//Receiver.h
#pragma once
class Receiver {
public:
~Receiver();
Receiver();
void doAction();
};
//Receiver.cpp
#include"Receiver.h"
#include<iostream>
Receiver::Receiver() {
}
Receiver::~Receiver() {
}
void Receiver::doAction() {
std::cout << "Receiver...action" << std::endl;
}
//Command.h
#pragma once
class Receiver;
class Command {
public:
virtual ~Command();
virtual void Excute() = 0;
protected:
Command();
};
class ConcreteCommand :public Command {
public:
~ConcreteCommand();
ConcreteCommand(Receiver* receiver);
void Excute();
private:
Receiver* receiver;
};
//Command.cpp
#include"Command.h"
#include"Receiver.h"
#include<iostream>
Command::Command() {
}
Command::~Command() {
}
ConcreteCommand::ConcreteCommand(Receiver* receiver) {
this->receiver = receiver;
}
ConcreteCommand::~ConcreteCommand() {
}
void ConcreteCommand::Excute() {
std::cout << "ConcreteCommand" << std::endl;
receiver->doAction();
}
//Invoke.h
#pragma once
class Command;
class Invoke {
public:
Invoke(Command* command);
~Invoke();
void Invoker();
private:
Command* command;
};
//Invoke.cpp
#include"Command.h"
#include"Invoke.h"
#include<iostream>
Invoke::Invoke(Command* command) {
this->command = command;
}
Invoke::~Invoke() {
}
void Invoke::Invoker() {
std::cout << "Invoke" << std::endl;
command->Excute();
}
//Main.cpp
#include"Invoke.h"
#include"Receiver.h"
#include"Command.h"
#include<iostream>
using namespace std;
int main() {
Receiver* receiver = new Receiver();
Command* command = new ConcreteCommand(receiver);
Invoke* invoke = new Invoke(command);
invoke->Invoker();
delete receiver;
delete command;
delete invoke;
return 0;
}
运行结果