(Boolan)C++设计模式 <十二> ——命令模式(Command)和访问器(Visitor)
来源:互联网 发布:淘宝网姓名贴 编辑:程序博客网 时间:2024/06/06 04:47
“行为变化”模式
在组建的构建过程中,组建行为的变化经常导致组建本身剧烈的变化。“行为变化”模式将组建的行为和组建本身进行解耦,从而主持组件的变化,实现两者之间的松耦合。
- 典型模式
- Command
- Visitor
命令模式Command
将一个请求(行为)封装为对象,从而使你可用不同的请求,对客户进行参数化;对请求排队或记录请求日志以及支持可撤销的操作。
——《设计模式》GoF
- 动机
在软件构建构成中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合——比如需要对行为进行“记录、撤销(undo)、事务”邓程澧,这种无法抵御变化的紧耦合是不合适的。
#include <iostream>#include <vector>#include <string>using namespace std;class Command{public: virtual void execute() = 0;};class ConcreteCommand1 : public Command{ string arg;public: ConcreteCommand1(const string & a) : arg(a) {} void execute() override { cout<< "#1 process..."<<arg<<endl; }};class ConcreteCommand2 : public Command{ string arg;public: ConcreteCommand2(const string & a) : arg(a) {} void execute() override { cout<< "#2 process..."<<arg<<endl; }};class MacroCommand : public Command{ vector<Command*> commands;public: void addCommand(Command *c) { commands.push_back(c); } void execute() override { for (auto &c : commands) { c->execute(); } }};int main(){ ConcreteCommand1 command1(receiver, "Arg ###"); ConcreteCommand2 command2(receiver, "Arg $$$"); MacroCommand macro; macro.addCommand(&command1); macro.addCommand(&command2); macro.execute();}
Command有一个execute的虚函数,派生了一系列的子类,由单一的命令,也有宏命令(用到了Composite模式,继承自Command,动态遍历了容器中的Command命令,以实现了一组命令的组合)。在使用层面,我们拿到的是对象,但是表征的却是行为。可以通过一些容器的存放对象的模式,来实现出类似于剪切、撤销等操作,只需要将对象弹出或者压入即可。
要点总结
- Command模式的根本目的在于“行为请求者”与“行为实现者”解耦,在面向对象的语言中,常见的实现手段是“将行为抽象为对象”
- 实现Command接口的具体命令对象ConcreteCommand有时候根据需要可能会保存一些额外的状态信息。通过使用Composite模式,可以将多个“命令”封装为一个“符合命令”MacroCommand
Command模式与C++中的函数对像有些类似。但两者定义行为接口的规范有所区别:Command以面向对象中的“接口-实现”来定义行为接口规范,更严格,但有性能损失;C++函数对象以函数签名来定义行为接口规范,更灵活,性能能高。
访问者Visitor
表示一个作用与某对像结构中的各元素的操作。使得可以在不改变(稳定)各元素的类的前提下定义(扩展)作用于这些元素的新操作(变化)。
——《设计模式》GoF
- 动机
在软件构建的过程中,由于需求的改变,某些类层次结构中常常需要增加新的行为(方法)。如果直接在类中做这样的更改,将会给子类带来很繁重的变更负担,甚至破坏原有设计。
#include <iostream>using namespace std;class Visitor;class Element{public: virtual void accept(Visitor& visitor) = 0; //第一次多态辨析 virtual ~Element(){}};class ElementA : public Element{public: void accept(Visitor &visitor) override { visitor.visitElementA(*this); }};class ElementB : public Element{public: void accept(Visitor &visitor) override { visitor.visitElementB(*this); //第二次多态辨析 }};class Visitor{public: virtual void visitElementA(ElementA& element) = 0; virtual void visitElementB(ElementB& element) = 0; virtual ~Visitor(){}};//==================================//扩展1class Visitor1 : public Visitor{public: void visitElementA(ElementA& element) override{ cout << "Visitor1 is processing ElementA" << endl; } void visitElementB(ElementB& element) override{ cout << "Visitor1 is processing ElementB" << endl; }};//扩展2class Visitor2 : public Visitor{public: void visitElementA(ElementA& element) override{ cout << "Visitor2 is processing ElementA" << endl; } void visitElementB(ElementB& element) override{ cout << "Visitor2 is processing ElementB" << endl; }};int main(){ Visitor2 visitor; ElementB elementB; elementB.accept(visitor);// double dispatch ElementA elementA; elementA.accept(visitor); return 0;}
当父类增加了新的操作,那么修改的代价极高,后续派生出来的所以子类都需要更改。违背了开闭原则。
应该是扩展新的需求该不是在修改的情况下添加新的操作。
#include <iostream>using namespace std;class Visitor;class Element{public: virtual void Func1() = 0; virtual void Func2(int data)=0; virtual void Func3(int data)=0; //... virtual ~Element(){}};class ElementA : public Element{public: void Func1() override{ //... } void Func2(int data) override{ //... }};class ElementB : public Element{public: void Func1() override{ //*** } void Func2(int data) override { //*** }};
Visitor的缺点:对于Visitor来说,不仅仅需要Vistor和Element需要稳定,同时也需要ConcreteElementA和ConcreteElement这两个类也保持稳定,而这个条件是很难保证的。如果新增加了Element的子类,那么Visitor的基类就需要改变,同时也会牵扯到ConcreteVisitor。所以这就是Vistor的缺点。Visitor的条件很难达成。
要点总结
- Vistor模式通过所谓的双重分发(double dispatch)来实现现在不更改(不添加新的操作-编译时)Element类层次结构的前提下,在运行时透明地为类层次结构上的各个类动态添加新的操作(支持变化)。
- 所谓双重分发即Vistor模式中包括了两个多态分发(注意其中的多态机制):第一个accept方法的多态解析;第二个visitElementX方法的多态解析。
- Visitor模式最大的缺点在于扩展类层次结构(增添新的Element子类),会导致Visitor类的改变。因此Visitor模式适用于“Element类层次结构稳定,而其中的操作却进场面临频繁改动”。
阅读全文
0 0
- (Boolan)C++设计模式 <十二> ——命令模式(Command)和访问器(Visitor)
- (Boolan)C++设计模式 <十三> ——解析器(Interpreter)
- (Boolan)C++设计模式 <三> ——策略模式(Strategy)
- (Boolan)C++设计模式 <四> ——观察者模式(Observer)
- (Boolan)C++设计模式 <五> ——装饰模式(Decorator)
- (Boolan)C++设计模式 <六> ——桥模式(Bridge)
- (Boolan)C++设计模式 <十> ——状态模式(State)和备忘录(Memento)
- (Boolan)C++设计模式 <二> ——模版方法(Template Method)
- (Boolan)C++设计模式 <一> ——设计模式简介以及面向对象设计原则
- (Boolan)C++设计模式 <九> ——单例模式(Singleton)和享元模式(FlyWeight)
- (Boolan)C++设计模式 <十一> ——组合模式(Composite)、迭代器(Iterator)和责任链(Chain of Resposibility)
- (Boolan)C++设计模式 <七> ——工厂方法(Factory Method)、 抽象工厂(Abstract Factory)、原型模式(Prototype)、构建器(Builder)
- (Boolan)C++设计模式 <八> ——门面模式(Facade)、代理模式(Proxy)、适配器(Adapter)以及中介者(Mediator)
- 设计模式C++之十二(Command命令模式)
- 设计模式之(二十二)访问者模式(visitor)
- 设计模式(二十)visitor访问器
- 设计模式(二十二)command
- 设计模式之——命令模式(Command)
- (Boolan)C++设计模式 <八> ——门面模式(Facade)、代理模式(Proxy)、适配器(Adapter)以及中介者(Mediator)
- Linux文件编程
- (Boolan)C++设计模式 <九> ——单例模式(Singleton)和享元模式(FlyWeight)
- (Boolan)C++设计模式 <十> ——状态模式(State)和备忘录(Memento)
- (Boolan)C++设计模式 <十一> ——组合模式(Composite)、迭代器(Iterator)和责任链(Chain of Resposibility)
- (Boolan)C++设计模式 <十二> ——命令模式(Command)和访问器(Visitor)
- codeforces722D
- (Boolan)C++设计模式 <十三> ——解析器(Interpreter)
- POJ 2689:Prime Distance
- Linux嵌入式开发入门(一)——初探嵌入式开发板的基本使用
- 安卓蓝牙流程
- Linux嵌入式开发入门(二)——快速看懂原理图,对接软件开发
- 嵌入式Linux驱动开发(二)——字符设备驱动之控制LED
- C++ usage of fstream