C++23种设计模式之桥模式,装饰模式

桥模式(Bridge)

桥模式

意图:将抽象部份与它的实现部份分离,使它们都可以独立地变化

装饰模式(Decorator)

装饰模式

  • 装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象
  • 既然可以动态增加功能,那么这功能也可以动态地被撤销,重点:扩展/增强 对象的功能 ,而非 限制 对象的功能
    在这里插入图片描述
    在这里插入图片描述
    代码
#include <bits/stdc++.h>

//
//装饰模式
//关键代码:
// 1.Component 类充当抽象角色,不应该具体实现
// 2.修饰类引用和继承 Component 类,具体扩展类重写父类方法。
//

using namespace std;

//抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
class Component {
public:
    virtual ~Component() = default;

    virtual void configuration() = 0;
};

//具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
class Car : public Component {
public:
    void configuration() override {
        std::cout << "A Car" << std::endl;
    }
};

//装饰(Decorator)角色:持有一个构件(Component)对象的实例,
// 并实现一个与抽象构件接口一致的接口。
class DecorateCar : public Component {
public:
    DecorateCar(std::shared_ptr<Component> car) : m_pCar(car) {}

    void configuration() override {
        m_pCar->configuration();
    }

private:
    std::shared_ptr<Component> m_pCar;
};

//具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。
class DecorateLED : public DecorateCar {
public:
    DecorateLED(std::shared_ptr<Component> car) : DecorateCar(car) {}

    void configuration() override {
        DecorateCar::configuration();
        //or add other somthing
        addLED();
    }

private:
    void addLED() {
        std::cout << "Install LED" << std::endl;
    }
};

//具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。
class DecoratePC : public DecorateCar {
public:
    DecoratePC(std::shared_ptr<Component> car) : DecorateCar(car) {}

    void configuration() override {
        DecorateCar::configuration();
        addPC();
    }

private:
    void addPC() {
        std::cout << "Install PC" << std::endl;
    }
};

//具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。
class DecorateEPB : public DecorateCar {
public:
    DecorateEPB(std::shared_ptr<Component> car) : DecorateCar(car) {}

    void configuration() override {
        DecorateCar::configuration();
        addEPB();
    }

private:
    void addEPB() {
        std::cout << "Install Electrical Park Brake" << std::endl;
    }
};

int main() {
    std::shared_ptr<Car> car = std::make_shared<Car>();
    std::shared_ptr<DecorateLED> ledCar = std::make_shared<DecorateLED>(car);//装饰一层
    ledCar->configuration();

    std::cout << std::string(30, '-') << std::endl;

    std::shared_ptr<DecoratePC> pcCar = std::make_shared<DecoratePC>(ledCar);//装饰二层
    std::shared_ptr<DecorateEPB> epbCar = std::make_shared<DecorateEPB>(pcCar);//装饰三层
    epbCar->configuration();

    return 0;
    //运行结果如下:
    //A Car
    //Install LED
    //------------------------------
    //A Car
    //Install LED
    //Install PC
    //Install Electrical Park Brake
}
  1. ConcreteComponent实现Component的接口,已有的功能(被装饰者)
  2. Decorator拥有ConcreteComponent对象,这里保证了已有的功能实现
  3. 定义一个与Component同样的接口Operation()
  4. DecoratorAImpl重写Operation(),内部调用父类Decorator(已有功能),在添加额外/新增的功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值