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

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.dtcms.com/a/288085.html

相关文章:

  • 教育科技内容平台的破局之路:从组织困境到 UGC 生态的构建
  • 我是怎么设计一个订单号生成策略的(库存系统)
  • 带root权限_新魔百和cm311-5_gk6323不分代工通刷优盘强刷及线刷
  • Openlayers 面试题及答案180道(141-160)
  • JavaScript 中的继承
  • MySQL——约束类型
  • 【RK3576】【Android14】分区划分
  • Java行为型模式---中介者模式
  • HOT100——排序篇Leetcode215. 数组中的第K个最大元素
  • 深度解析 rag-vector-agent-semantic-kernel:基于 Semantic Kernel 的 Agentic RAG 实践
  • 变频器实习Day10
  • JS原型相关知识
  • EINO框架解读:字节跳动开源的大模型应用开发框架
  • 【jquery详细讲解】
  • Vue Swiper组件
  • Vue组件化开发小案例
  • 在开发板tmp目录下传输文件很快的原因和注意事项:重启开发板会清空tmp文件夹,记得复制文件到其他地方命令如下(cp 文件所在路径 文件要复制到的路径—)
  • GitLab 社区版 10.8.4 安装、汉化与使用教程
  • GPU集群如何规划
  • 子串算法题
  • Web攻防-身份验证篇JWT令牌空密钥未签名密钥爆破JWKJWUKID算法替换CVE报告复盘
  • 在Vscode中使用Kimi K2模型:实践指南,三分钟生成个小游戏
  • TypeScript 中的「类」:从语法到实战的完整指南
  • 论C/C++的条件编译#if、#ifdef、#ifndef、#undef
  • Promise入门
  • 三级知识点汇总(详解)【c++】——2
  • 我用Cursor,1周上线了一个虚拟资料流量主小程序技术选型
  • Linux“一切皆文件“设计哲学 与 Linux文件抽象层:struct file与file_operations的架构解析
  • 【ChatOpenAI】常用方法详解
  • HOT100——动态规划篇Leetcode221. 最大正方形