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

设计模式——装饰者模式(Decorator)

装饰者模式(Decorator Pattern)是一种结构型设计模式,它允许你动态地给一个对象添加一些额外的职责,就增加功能来说,装饰者模式相比生成子类更为灵活。在装饰者模式中,一个装饰类会包装一个对象(通常称为被装饰者),并为其添加一些新的功能。

装饰者模式包含以下几个角色:

  1. Component(抽象组件):定义一个对象接口,可以给这些对象动态地添加职责。
  2. ConcreteComponent(具体组件):实现Component接口,是装饰器可以装饰的对象。
  3. Decorator(抽象装饰器):继承自Component,持有对Component对象的引用,并定义与Component接口一致的接口。
  4. ConcreteDecorator(具体装饰器):实现Decorator接口,负责给Component添加职责。

装饰者模式的特点:

  • 装饰者和被装饰者对象有相同的超类型(接口或者抽象类)。
  • 你可以用一个或多个装饰者包装一个对象。
  • 装饰者可以在所委托被装饰者的行为之前与/或之后,加上自己的行为,以达到特定的目的。
  • 对象可以在任何时候被装饰,所以可以在运行时动态地、不限量地用你喜欢的装饰者来装饰对象。

以下是一个简单的Java装饰者模式示例:

// 抽象组件(Component)
public interface Component {void operation();
}// 具体组件(ConcreteComponent)
public class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("执行基础操作");}
}// 抽象装饰器(Decorator)
public abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {if (component != null) {component.operation();}}
}// 具体装饰器A(ConcreteDecoratorA)
public class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedFunctionA();}public void addedFunctionA() {System.out.println("为操作添加功能A");}
}// 具体装饰器B(ConcreteDecoratorB)
public class ConcreteDecoratorB extends Decorator {public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedFunctionB();}public void addedFunctionB() {System.out.println("为操作添加功能B");}
}// 客户端(Client)
public class Client {public static void main(String[] args) {Component component = new ConcreteComponent();// 使用装饰器A包装ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(component);decoratorA.operation(); // 执行基础操作,并添加功能A// 使用装饰器B包装装饰器AConcreteDecoratorB decoratorB = new ConcreteDecoratorB(decoratorA);decoratorB.operation(); // 执行基础操作,添加功能A,并再添加功能B}
}

在这个例子中,ConcreteComponent是具体组件,它实现了Component接口中的operation()方法。Decorator是抽象装饰器,它持有一个Component对象的引用,并提供了operation()方法的默认实现,即调用被装饰者的operation()方法。ConcreteDecoratorAConcreteDecoratorB是具体装饰器,它们分别添加了不同的功能(addedFunctionA()addedFunctionB())。在客户端代码中,你可以看到如何使用装饰器来动态地给对象添加职责。

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

相关文章:

  • 力扣:406. 根据身高重建队列
  • Docker 怎么将映射出的路径设置为非root用户权限
  • Linux——进程的优先级、ACL
  • 【C++】STL-list模拟实现
  • Java 7大排序
  • vue3 - 图灵
  • java设计模式八 享元
  • ELK原理详解
  • 多线程学习Day09
  • 第33次CSP认证Q1:词频统计
  • pytorch加载模型出现错误
  • 如何在Mac上恢复格式化硬盘的数据?
  • 华为OD机试 - 手机App防沉迷系统(Java 2024 C卷 100分)
  • 搜维尔科技:光学动作捕捉系统用于城市公共安全智慧感知实验室
  • 保研面试408复习 4——操作系统、计网
  • 实战攻防中关于文档的妙用
  • 【使用ChatGPT的API之前】OpenAI API提供的可用模型
  • 【C语言】模拟实现深入了解:字符串函数
  • 钩子函数onMounted定义了太多访问MySQL的操作 导致数据库异常
  • Excel文件解析---超大Excel文件读写
  • TypeScript基础:类型系统介绍
  • 【Unity】Unity项目转抖音小游戏(一) 项目转换
  • element-ui 中修改loading加载样式
  • QT登录界面,(页面的切换)
  • 计算机毕业设计 | vue+springboot汽车销售管理系统(附源码)
  • 一款开源的原神工具箱,专为现代化 Windows 平台设计,旨在改善桌面端玩家的游戏体验
  • python日常消费数据占比分析总结年消费方向
  • MySQL变量的浮点数问题处理
  • MWeb Pro for Mac:功能强大的Markdown博客编辑器
  • 基于FPGA实现的HDMI TO MIPI扩展显示器方案