博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Command模式
阅读量:7007 次
发布时间:2019-06-27

本文共 3782 字,大约阅读时间需要 12 分钟。

hot3.png

                    命令(Command)模式属于对象的行为模式【GOF95】。命令模式又称为行动(Action)模式或交易(Transaction)模式。命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

  适用性:在软件系统中,行为请求者与行为实现者之间通常呈现一种紧耦合的关系。但在某些场合,比如要对行为进行记录撤销重做事务等处理,这种无法抵御变化的紧耦合是不合适的。这种情况下,使用command模式将行为请求者与行为实现者进行解耦。

 

                    这是我举例子的类图(比较挫...)

                   

 

                       代码实现:

#ifndef OPERATOR_H

#define OPERATOR_H
 
enum Operator {
FORWARD, BACK};
 
#endif // OPERATOR_H
 

 

#ifndef COMMAND_H

#define COMMAND_H
 
#include "Operator.h"
#include 
 
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 
 
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 
#include "command.h"
 
class Invoker
{
public:
Invoker();
~Invoker();
void invoke(Operator _com);
void addCommand(Command *_com);
private:
std::vector
CommandList;
 
};
 
#endif // INVOKER_H
#include "invoker.h"
 
Invoker::Invoker()
{
}
Invoker::~Invoker()
{
std::vector
::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
::iterator ite;
for (ite = CommandList.begin();ite != CommandList.end();++ite) {
if ((*ite)->getOperator()== _com) {
(*ite)->operation();
}
}
}
#include 
 
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;
}
 
 
 
 
 

 

转载于:https://my.oschina.net/u/854744/blog/418182

你可能感兴趣的文章
Spring Cloud OAuth2 资源服务器CheckToken 源码解析
查看>>
jQuery DOM操作
查看>>
高频写入redis场景优化
查看>>
一直在做业务的程序员技术会进步吗?我们该如何跳出舒适圈
查看>>
Promise 源码分析
查看>>
mobx
查看>>
C++ Primer 第三章 学习笔记及习题答案
查看>>
Lodash学习小记
查看>>
webpack4 系列教程(十六):开发模式和生产模式·实战
查看>>
Elasticsearch 参考指南(查询和过滤器上下文)
查看>>
python 历险记——一个 Java 程序员的告白(一)
查看>>
AliOS Things手势识别应用演示
查看>>
EOS入门指南 - PART1 环境搭建
查看>>
函数式编程了解一下(下)
查看>>
python 数据类型 - dict 字典
查看>>
flutter环境搭建及跑起来demo(多图慎入)
查看>>
ubuntu 织梦DEDE安装 GD插件 OFF问题
查看>>
Netty-ChannelHandler-ChannelPipeline
查看>>
php 上传图片造成内存溢出 Allowed memory size of ... bytes
查看>>
[Doctrine Migrations]数据库迁移组件的深入解析一:安装与使用
查看>>