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

设计模式-装饰器模式

定义

装饰器模式是一种结构型设计模式,允许在不改变对象结构的情况下,动态地为对象添加新功能。
概念:
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种模式通过将对象包装在装饰器类中,以便动态地修改其行为。装饰器模式提供了一种灵活的替代继承方式来扩展功能,避免通过继承引入静态特征,特别是在子类数量急剧膨胀的情况下。

结构

  1. 组件接口(Component):定义一个接口,规定可以动态添加职责的对象标准。
  2. 具体组件(ConcreteComponent):实现该接口的具体类,提供基本功能。
  3. 抽象装饰者(Decorator):实现同样的接口,持有一个组件接口的引用,可以在任何时候动态的添加功能。
  4. 具体装饰者(ConcreteDecorator):扩展抽象装饰者,添加额外的职责。

应用场景

装饰器模式是一种强大的设计模式,适用于需要动态扩展对象功能的场景,能够有效提高代码的灵活性和可维护性。

  1. 动态添加功能:当需要在运行时动态的添加或者撤销对象的功能时,装饰器模式非常有用
  2. 避免类爆炸:在需要扩展类的功能但又不想通过继承的方式来实现时,装饰器模式可以有效避免类的数量急剧增加。
  3. 增强现有功能:可以在不修改现有代码情况下,增强现有功能。

示例

假设我们有一个基本的咖啡类 Coffee,我们可以通过装饰器模式为其添加不同的配料,如牛奶、糖等,而不需要为每种组合创建新的子类。每个配料都可以作为一个具体装饰者,动态地添加到咖啡对象中。
代码

/*** @ClassName Coffee* @Description 组件接口* @Author Feng* @Date 2025/6/15**/
public interface Coffee {double cost();String getDescription();
}
class SimpleCoffee implements Coffee {@Overridepublic double cost() {return 2.;}@Overridepublic String getDescription() {return "没加任何东西的咖啡";}
}
abstract class CoffeeDecorator implements Coffee{protected Coffee coffee;public CoffeeDecorator(Coffee coffee) {this.coffee = coffee;}@Overridepublic double cost() {return coffee.cost();}@Overridepublic String getDescription() {return coffee.getDescription();}
}
class MilkCoffee extends CoffeeDecorator{public MilkCoffee(Coffee coffee) {super(coffee);}@Overridepublic double cost() {return 4. + coffee.cost();}@Overridepublic String getDescription() {return coffee.getDescription() + "加了奶";}
}
class SugarCoffee extends CoffeeDecorator{public SugarCoffee(Coffee coffee) {super(coffee);}@Overridepublic double cost() {return 3. + coffee.cost();}@Overridepublic String getDescription() {return coffee.getDescription() + "加了糖";}
}
public class DecoratorTest {public static void main(String[] args) {SimpleCoffee simpleCoffee = new SimpleCoffee();System.out.println(simpleCoffee.getDescription() + " \t价格为:" + simpleCoffee.cost());// 加了糖的咖啡Coffee sugarCoffee = new SugarCoffee(simpleCoffee);System.out.println(sugarCoffee.getDescription() + " \t价格为:" + sugarCoffee.cost());// 加了奶的咖啡Coffee milkCoffee = new MilkCoffee(simpleCoffee);System.out.println(milkCoffee.getDescription() + " \t价格为:" + milkCoffee.cost());// 加了糖和奶的咖啡Coffee sugarMilkCoffee = new SugarCoffee(milkCoffee);System.out.println(sugarMilkCoffee.getDescription() + " \t价格为:" + sugarMilkCoffee.cost());}
}

在这里插入图片描述
优点
灵活性:可以在运行时动态的添加或者修改对象的功能
可扩展性:避免了通过继承引入的复杂性,提供了更好的扩展性和维护性。

http://www.xdnf.cn/news/14404.html

相关文章:

  • React-router 路由历史的模式和原理
  • AI 神经网略小白学习笔记(一) -- 环境搭建
  • 【1】Redis 缓存穿透原理和解决方案
  • [AAAI Oral] 简单通用的公平分类方法
  • React-router 多类型历史记录栈
  • 《仿盒马》app开发技术分享-- 回收金提现安全锁校验(端云一体)
  • NodeJS中老生代和新生代和垃圾回收机制
  • Arduino入门教程:6、计时与定时
  • 阿帕奇基金会软件授权与公司贡献者许可协议(中英双语版)
  • (笔记)1.web3学习-区块链技术
  • Web3-代币ERC20/ERC721以及合约安全溢出和下溢的研究
  • EXCEL破解VBA密码 ( 仅供学习研究使用)
  • [VSCode] VSCode 设置 python 的编译器
  • 40-Oracle 23 ai Bigfile~Smallfile-Basicfile~Securefile矩阵对比
  • NodeJS里经常用到require,require的模块加载机制是什么
  • lua版的Frpc
  • go.work
  • 车载通信架构 --- IP ECU 在连接被拒绝后的重连机制
  • Spring Cloud Gateway 全面学习指南
  • 论文略读:MLPs Learn In-Context on Regression and Classification Tasks
  • CM工作室发展史 下
  • Python装饰器:优雅增强函数行为的艺术
  • AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月14日第108弹
  • Win10安装DockerDesktop踩坑记
  • Java学习_‘+’作连接符的情况
  • Go语言底层(五): 深入浅出Go语言的ants协程池
  • ASR语音转写技术全景解析:从原理到实战
  • shell三剑客
  • FileBrowser Quantum更丝滑的文件网盘共享FileBrowser的平替
  • Python命名空间与作用域:深入解析名称查找的艺术