当前位置: 首页 > backend >正文

6. 装饰器模式

目录

  • 一、简介
  • 二、类图
  • 三、代码实现
    • 3.1 设计类图
    • 3.2 代码实现

一、简介

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

二、类图

在这里插入图片描述

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

三、代码实现

3.1 设计类图

以不同型号的车的装饰品为例
在这里插入图片描述

3.2 代码实现

#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;
}
http://www.xdnf.cn/news/15813.html

相关文章:

  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘pillow’问题
  • 小架构step系列19:请求和响应
  • Java行为型模式---中介者模式
  • [故障诊断方向]SNNs:针对小样本轴承故障诊断的孪生神经网络模型
  • Selenium 中 findElement 方法全解析:定位网页元素的 7 种方式
  • BeanFactory 和 FactoryBean 的区别
  • Java行为型模式---访问者模式
  • 用Dynamic chunk去干掉tokenizer?
  • 从零入门:云迁移原理详解与华为Rainbow实战指南
  • 数据结构 队列
  • 信息系统风险的安全技术防范思路
  • 教育科技内容平台的破局之路:从组织困境到 UGC 生态的构建
  • CCF编程能力等级认证GESP—C++7级—20250628
  • [FFmpeg] AVFormatContext、AVInputFormat、AVOutputFormat | libavformat
  • 为任意Java程序配置Socks5与HTTP代理的方法
  • 2025年水安备考:水利水电安全员C类考试题
  • 基于Scrapy-Redis的分布式爬虫系统:工业级实现与深度优化
  • nodejs值process.kill
  • CCF编程能力等级认证GESP—C++8级—20250628
  • 信息学奥赛一本通 1579:【例 5】皇宫看守 | 洛谷 P2458 [SDOI2006] 保安站岗
  • 教你如何借助AI精读文献
  • MC0463四大名著-水浒签到
  • 在Vscode中使用Kimi K2模型:实践指南,三分钟生成个小游戏
  • 网络大提速,RDMA,IB,iWrap
  • 深度学习中的模型剪枝工具Torch-Pruning的使用
  • 如何解决AttributeError: ‘NoneType‘ object has no attribute问题
  • 使用 PlanetScope 卫星图像绘制水质参数:以莫干湖为例
  • 记录我coding印象比较深刻的BUG
  • 【Docker项目实战】使用Docker部署Homeland社区系统
  • 以太坊的心脏与大脑:详解执行客户端(EL)与共识客户端(CL)