[A].总结9.行为变化模式:命令模式,访问器

总结9.行为变化模式:命令模式,访问器

声明:本栏目的 [A] 系列的学习笔记,学习对象为 B 站授课视频 C++设计模式(李建忠),参考教材为《设计模式:可复用面向对象软件的基础》。本栏目 [A] 系列文章中的图件和笔记,部份来自上述资源。

从封装变化角度对模式分类!:

  • 组件协作
  • 单一职责
  • 对象创建
  • 对象性能
  • 接口隔离
  • 状态变化
  • 数据结构
  • 行为变化
    在组件的构建过程中,组件行为的变化经常导致组件本身剧烈的变化。“行为变化”模式将组件的行为和组件本身进行解耦,从而支持组件行为的变化,实现两者之间的松耦合。
    典型模式
    • 命令模式 Command
    • 访问器 VIsitor
  • 领域问题
命令模式 Command

在这里插入图片描述

  • 定义:将一个请求(行为)封装成一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
  • 代码举例:
#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("Arg ###");
    ConcreteCommand2 command2("Arg $$$");

    MacroCommand macro; // 命令组合
    macro.addCommand(&command1);
    macro.addCommand(&command2);

    macro.execute();
    return 0;
}
  • execute 为虚函数,运行时绑定,性能损失。
  • C++ 函数对象通常比命令模式更好用,搭配模板使用,编译时绑定,性能更高。(模板是编译时多态,虚函数是运行时多态)。C++性能优先,主流编程中,函数对象一般都替代了命令模式。
  • 命令模式在C++以外的语言中,还是有很多使用的。
  • 有人说,设计模式是对编程语言不足的弥补。
    在这里插入图片描述
访问器 VIsitor

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 代码举例:
#include <iostream>
using namespace std;

class ElementA;
class ElementB;

class Visitor{
public:
    virtual void visitElementA(ElementA& element) = 0;
    virtual void visitElementB(ElementB& element) = 0;

    virtual ~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); //第二次多态辨析
    }

};

//================================== 以上部分稳定不变,以下部分为易变化部分

//扩展1
class 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;
    }
};

//扩展2
class 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;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值