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

设计模式笔记_结构型_装饰器模式

1.装饰器模式介绍

装饰器模式是一种结构型设计模式,允许你动态地给对象添加行为,而无需修改其代码。它的核心思想是将对象放入一个“包装器”中,这个包装器提供了额外的功能,同时保持原有对象的接口不变。

想象一下,你有一个简单的咖啡,你想让它变得更特别。你可以给它加奶、加糖、加香草等等,但咖啡本身还是咖啡。这些额外的东西不会改变咖啡的本质,只是让它更丰富。

装饰器模式有四个角色:

  1. 抽象组件(Component):可以是一个接口或者抽象类,规定了被装饰对象的行为;
  2. 具体组件(ConcreteComponent):实现或继承Component的一个具体对象,也即被装饰对象
  3. 抽象装饰器(Decorator):一般是抽象类, 继承或实现抽象组件Component;其内部必然有一个属性指向Component组件对象;通过其子类 ConcreteDecorator 扩展具体构件的功能。
  4. 具体装饰器(ConcreteDecorator):Decorator的具体实现类,理论上每个ConcreteDecorator 都扩展了 Component 对象的一种功能;

四个角色的关系:

2.代码演示

抽象组件(Component):咖啡接口定义了咖啡有“描述”和“价格”两个方法

// 基础接口
interface Coffee {String getDescription();double getCost();
}

具体组件(ConcreteComponent): 咖啡实现类,是需要被装饰的对象

// 具体的咖啡类
class SimpleCoffee implements Coffee {@Overridepublic String getDescription() {return "Simple Coffee";}@Overridepublic double getCost() {return 5.0;}
}

抽象装饰器(Decorator):抽象类,实现咖啡接口,内部有一个属性指向Coffee对象

// 装饰器基类
abstract class CoffeeDecorator implements Coffee {//内部必然有一个属性指向Component组件对象protected Coffee coffee;public CoffeeDecorator(Coffee coffee) {this.coffee = coffee;}@Overridepublic String getDescription() {return coffee.getDescription();}@Overridepublic double getCost() {return coffee.getCost();}
}

具体装饰器(ConcreteDecorator):每个Coffee装饰器都扩展了Coffee对象的一种功能

// 具体的装饰器类
class MilkDecorator extends CoffeeDecorator {public MilkDecorator(Coffee coffee) {super(coffee);}@Overridepublic String getDescription() {return coffee.getDescription() + ", Milk";}@Overridepublic double getCost() {return coffee.getCost() + 1.5;}
}class SugarDecorator extends CoffeeDecorator {public SugarDecorator(Coffee coffee) {super(coffee);}@Overridepublic String getDescription() {return coffee.getDescription() + ", Sugar";}@Overridepublic double getCost() {return coffee.getCost() + 0.5;}
}

使用装饰器:每个装饰器都可以单独或组合使用,给咖啡增加不同的特性。通过这种方式,原来的咖啡类保持不变,而我们可以灵活地为它添加新功能

// 使用装饰器模式
public class CoffeeShop {public static void main(String[] args) {//创建简单的咖啡类SimpleCoffeeCoffee coffee = new SimpleCoffee();System.out.println(coffee.getDescription() + " $" + coffee.getCost());//通过装饰器MilkDecorator给咖啡加奶coffee = new MilkDecorator(coffee);System.out.println(coffee.getDescription() + " $" + coffee.getCost());//通过装饰器SugarDecorator给咖啡加糖coffee = new SugarDecorator(coffee);System.out.println(coffee.getDescription() + " $" + coffee.getCost());}
}

http://www.lryc.cn/news/590992.html

相关文章:

  • 【后端】.NET Core API框架搭建(9) --配置使用Log4Net日志
  • 人工智能之数学基础:概率论和数理统计在机器学习的地位
  • 使用Proxy设计模式来增强类的功能:ToastProxy和DesktopToast的设计关系
  • 力扣119:杨辉三角Ⅱ
  • UGUI 性能优化系列:第一篇——基础优化与资源管理
  • 取消office word中的段落箭头标记
  • 【图像处理基石】如何入门色彩评估?
  • Python暑期学习笔记3
  • Redis:哨兵(Sentinel)
  • 20250717 Ubuntu 挂载远程 Windows 服务器上的硬盘
  • 7.事务操作
  • 自动化技术在造纸行业的应用:EtherCAT转PROFIBUS DP解决方案
  • 简单手写一个Spring boot starter
  • Java中excel字典转换
  • clonezilla 导出自动化恢复iso
  • 网络初级安全第二次作业
  • iOS WebView 调试与性能优化 跨平台团队高效协作方法解析
  • 【前端】Power BI自动化指南:从API接入到Web嵌入
  • 汽车功能安全 -- TC3xx外部看门狗
  • 可复用软件的构建本质是组织能力的重构
  • 灵易智模中的重构导出可以做什么
  • Elasticsearch / MongoDB / Redis / MySQL 区别
  • Odoo最佳业务实践:从库存管理重构到全链路协同
  • 重构比特币在 Sui DeFi 中的角色
  • Sentinel配置Nacos持久化
  • 个体认知的时域性与“与时俱进”的认知重构:一种历史意识下的认知演化分析
  • Redis4缓存穿透:布隆过滤器与空对象方案
  • HTML 极简个人介绍卡片(侧重语义化标签和响应式布局)
  • 单例模式详细讲解
  • 哈希表法求环形链表