6. 装饰器模式

目录

一、简介

  • 模式中的角色
    • 装饰者(decorator):用来装饰别的对象的对象
    • 被装饰者(decoratee):被装饰的对象
  • 解决的问题
    • 动态的给一个对象添加一些额外的功能和职责

二、类图

  • Component:被装饰对象的抽象父类
  • ConcreteComponent:等待被装饰对象的实体类
  • Decorator:装饰品抽象父类
  • ConcreteDecoratorA:装饰品实体类

三、代码实现

3.1 设计类图

以不同型号的车的装饰品为例

3.2 代码实现

cpp 复制代码
#include <iostream>

class Car
{
protected:
    unsigned int cost;
    std::string description;

public:
    virtual std::string getDescription() = 0;

    virtual unsigned int getCost() = 0;
};

class A1Car : public Car
{
public:
    A1Car(unsigned int cost, const std::string& description)
    {
        this->cost = cost;
        this->description = description;
    }

    std::string getDescription()
    {
        return this->description;
    }

    unsigned int getCost()
    {
        return this->cost;
    }

};

class A4Car : public Car
{
public:
    A4Car(unsigned int cost, const std::string& description)
    {
        this->cost = cost;
        this->description = description;
    }

    std::string getDescription()
    {
        return this->description;
    }

    unsigned int getCost()
    {
        return this->cost;
    }
};

class Decorator : public Car
{
protected:
    Car* m_car;
    unsigned int dec_cost;  //配件的价格
    std::string dec_description; //配件的描述
public:

    virtual std::string getDescription()
    {
        return m_car->getDescription() + this->dec_description;
    }

    virtual unsigned int getCost()
    {
        return this->dec_cost + m_car->getCost();
    }
};

class GPS : public Decorator
{
public:
    GPS(unsigned int cost, const std::string& description, Car* obj)
    {
        //std::cout << "new GPS: " << cost << std::endl;
        this->dec_cost = cost;
        this->dec_description = description;
        this->m_car = obj;
    }
};

class Redar : public Decorator
{
public:
    Redar(unsigned int cost, const std::string& description, Car* obj)
    {
        //std::cout << "new Redar: " << cost << std::endl;
        this->dec_cost = cost;
        this->dec_description = description;
        this->m_car = obj;
    }

};

int main()
{
    Car* a1 = new A1Car(10000, "A1Car");
    std::cout << "a1 cost:" << a1->getCost() << "; description: " << a1->getDescription() << std::endl;

    Car* a4 = new A4Car(15000, "A4Car");
    std::cout << "a4 cost:" << a4->getCost() << "; description: " << a4->getDescription() << std::endl;
    
    //给a4一个GPS配置
    a4 = new GPS(101, ",by GPS(RMB: 101)", a4);

    //再给a4一个Redar配置
    a4 = new Redar(98, ",by redar(RMB: 98)", a4);

    std::cout << "===========a1没加附加组件,因此价格不变===============" << std::endl;
    std::cout << a1->getDescription() << ". Cost: " << a1->getCost() << std::endl;
    std::cout << "===========a4加了GPS和redar两个组件,因此相对较贵=====" << std::endl;
    std::cout << a4->getDescription() << ". Cost: " << a4->getCost() << std::endl;

    return 0;
}
相关推荐
researcher-Jiang7 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
饼干哥哥15 小时前
用Claude Code跑通跨境电商的5大场景,顶10个人的团队
人工智能·设计模式·正则表达式
折哥的程序人生 · 物流技术专研16 小时前
第3篇:手写促销策略引擎(满减、打折、立减)
java·设计模式·策略模式·开闭原则·编程实战·扩充系列·促销引擎
折哥的程序人生 · 物流技术专研18 小时前
第3篇:手写一个饮品制作模板(附代码)
java·设计模式·行为型模式·模版方法模式·钩子方法·代码复用·编程实战
ttod_qzstudio19 小时前
【软考设计模式】单例模式:唯一实例的管控与线程安全精讲
单例模式·设计模式
折哥的程序人生 · 物流技术专研21 小时前
第4篇:模板方法 vs 策略模式,面试怎么选?
java·设计模式·策略模式·行为型模式·模版方法模式·扩充系列·继承v组合
小园子的小菜21 小时前
Java设计模式精讲:创建型+结构型模式(含生产级案例+相似模式对比)
学习·设计模式
geovindu2 天前
java:Abstract Factory Pattern
java·开发语言·后端·设计模式·抽象工厂模式·创建型模式
geovindu2 天前
java: Factory Method Pattern
java·开发语言·后端·设计模式·工厂方法模式·创建型模式
折哥的程序人生 · 物流技术专研2 天前
第2篇:模板方法模式核心:好莱坞原则
设计模式·uml·模板方法模式·行为型模式·钩子方法·扩充系列·好莱坞原则