適用性:在軟件系統中,行爲請求者與行爲實現者之間一般呈現一種緊耦合的關係。但在某些場合,好比要對行爲進行記錄撤銷重作事務等處理,這種沒法抵禦變化的緊耦合是不合適的。這種狀況下,使用command模式將行爲請求者與行爲實現者進行解耦。ios
這是我舉例子的類圖(比較挫...)spa
代碼實現:日誌
#ifndef OPERATOR_Hcode
#define OPERATOR_H
enum Operator {FORWARD, BACK};
#endif // OPERATOR_H
#ifndef COMMAND_H對象
#define COMMAND_H
#include "Operator.h"
#include <iostream>
class Command
{
public:
Command(Operator _operator);
virtual ~Command() {std::cout <<"c~\n";}
virtual void operation() = 0;
Operator getOperator() const {return _comOperator;}
private:
Operator _comOperator;
};
#endif // COMMAND_H
#include "command.h"事務
Command::Command(Operator _operator):_comOperator(_operator)
{
}
#ifndef FORWARDCOMMAND_H內存
#define FORWARDCOMMAND_H
#include "command.h"
#include "receiver.h"
class ForwardCommand : public Command
{
public:
ForwardCommand(Operator _operator, Receiver *_rev);
~ForwardCommand(){delete rev;}
void operation();
private:
Receiver *rev;
};
#endif // FORWARDCOMMAND_H
#include "forwardcommand.h"ForwardCommand::ForwardCommand(Operator _operator, Receiver *_rev):Command(_operator),rev(_rev){
}
void ForwardCommand::operation(){
rev->execute();}
#ifndef BACKCOMMAND_H#define BACKCOMMAND_H#include "command.h"#include "receiver.h"class BackCommand : public Command{
public:BackCommand(Operator _com, Receiver* _rev);~BackCommand(){delete rev;}void operation();private:Receiver *rev;};
#endif // BACKCOMMAND_H#include "backcommand.h"BackCommand::BackCommand(Operator _com, Receiver *_rev):Command(_com),rev(_rev){
}
void BackCommand::operation(){
rev->execute();}
#ifndef RECEIVER_H#define RECEIVER_H#include <iostream>class Receiver{
public:Receiver();virtual ~Receiver(){std::cout <<"r~\n";}virtual void execute() = 0;};
#endif // RECEIVER_H#include "receiver.h"Receiver::Receiver(){
}
#ifndef FORWARDRECEIVER_H#define FORWARDRECEIVER_H#include "receiver.h"class ForwardReceiver : public Receiver{
public:ForwardReceiver();~ForwardReceiver(){}void execute();};
#endif // FORWARDRECEIVER_H#include "forwardreceiver.h"ForwardReceiver::ForwardReceiver(){
}
void ForwardReceiver::execute(){
std::cout <<"ForwardCommand\n";}
#ifndef BACKRECEIVER_H#define BACKRECEIVER_H#include "receiver.h"class BackReceiver : public Receiver{
public:BackReceiver();~BackReceiver(){}void execute();};
#endif // BACKRECEIVER_H#include "backreceiver.h"BackReceiver::BackReceiver(){
}
void BackReceiver::execute(){
std::cout <<"backCommand\n";}
#ifndef INVOKER_H#define INVOKER_H#include "Operator.h"#include <vector>#include "command.h"class Invoker{
public:Invoker();~Invoker();void invoke(Operator _com);void addCommand(Command *_com);private:std::vector<Command*> CommandList;};
#endif // INVOKER_H#include "invoker.h"Invoker::Invoker(){
}
Invoker::~Invoker(){
std::vector<Command*>::iterator ite;for (ite = CommandList.begin();ite != CommandList.end();++ite) {delete (*ite);}CommandList.clear();}
void Invoker::addCommand(Command *_com){
//這裏能夠作一些判斷防止相同的對象生成CommandList.push_back(_com);}
void Invoker::invoke(Operator _com){
std::vector<Command*>::iterator ite;for (ite = CommandList.begin();ite != CommandList.end();++ite) {if ((*ite)->getOperator()== _com) {(*ite)->operation();}}}
#include <iostream>using namespace std;#include "invoker.h"#include "backcommand.h"#include "backreceiver.h"#include "forwardcommand.h"#include "forwardreceiver.h"int main(){
Invoker in;in.addCommand(new ForwardCommand(FORWARD,new ForwardReceiver));//不建議這樣寫,容易形成內存泄漏in.addCommand(new BackCommand(BACK,new BackReceiver));in.invoke(FORWARD);return 0;}