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

设计模式03-装饰模式(Java)

4.4 装饰模式

1.模式定义

不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式。

2.模式结构

image-20241013161535947

抽象构件角色 :定义一个抽象接口以规范准备接收附加责任的对象。客户端可以方便调用装饰类和被装饰类。

具体构件角色 :实现抽象构件,通过装饰角色为其添加一些职责。

抽象装饰角色 : 继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。

具体装饰角色 :实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。

3.模式原理

在不改变对象本身的基础之上,给对象添加或删除行为,往往可以通过继承机制或者是关联机制实现

  • 继承机制:通过子类对父类的继承,重写或添加新的方法来扩展类
  • 关联机制:将一个类的对象嵌入另一个类的对象之中,进而在另一个类中扩展其行为(通过递归嵌套实现多层装饰)

4.代码模板

抽象装饰类

public class Decorator implements Component {//关联抽象构建private Component component;//构造注入具体构建public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();}
}

具体装饰类

public class ConcreteDecorator extends Decorator{public ConcreteDecorator(Component component) {super(component);}public void operation() {super.operation();addBehavior();}private void addBehavior() {//新增方法}
}

5.案例分析

image-20241013173014212

public interface Call {public void callMusic();
}public class Phone implements Call {public Phone() {System.out.println("普通手机");}@Overridepublic void callMusic() {System.out.println("来电话了,手机发出响声");}
}public class Decorator implements Call{private Call call;public Decorator(Call call) {this.call = call;}public void setCall(Call call) {this.call = call;}@Overridepublic void callMusic() {System.out.println("来电话了,手机发出响声");}
}public class JarPhone extends Decorator {public JarPhone(Call call) {super(call);System.out.println("振动手机");}public void jar() {super.callMusic();System.out.println("振动~~~");}
}public class LightPhone extends Decorator{public LightPhone(Call call) {super(call);System.out.println("闪光手机");}public void light() {super.callMusic();System.out.println("闪光~~~");}
}public class Main {public static void main(String[] args) {Phone phone = new Phone();System.out.println("电话来了");phone.callMusic();System.out.println("————————————————");JarPhone jarPhone = new JarPhone(phone);System.out.println("电话来了");jarPhone.jar();System.out.println("————————————————");LightPhone lightPhone = new LightPhone(phone);System.out.println("电话来了");lightPhone.light();System.out.println("————————————————");//将闪光手机改装成可以振动且闪光的手机System.out.println("组装手机:");lightPhone.light();jarPhone.setCall(lightPhone);jarPhone.jar();}
}

6.模式优缺点

image-20241013175750571

7.模式使用场景

image-20241013175846869

8.模式应用

  • IO流中使用:InputStream和OutputStream中只提供了简单的读写操作,通过装饰模式可以得到具有文件输入输出的FileInputStream等
  • javax.swing中也有大量使用
http://www.lryc.cn/news/459599.html

相关文章:

  • TiDB 监控组件之 Blackbox_exporter 运行原理
  • Java之网络编程详解
  • 苍穹外卖学习笔记(二十)
  • 2024 第一次周赛
  • 【数据脱敏方案】不使用 AOP + 注解,使用 SpringBoot+YAML 实现
  • dbt doc 生成文档命令示例应用
  • 【Windows】【DevOps】Windows Server 2022 安装ansible,基于powershell实现远程自动化运维部署 入门到放弃!
  • 深入理解 Parquet 文件格式
  • 计算机挑战赛3
  • 深度学习:循环神经网络—RNN的原理
  • 蓝桥杯刷题--幸运数字
  • Node.js入门——fs、path模块、URL端口号、模块化导入导出、包、npm软件包管理器
  • 多元线性回归:机器学习中的经典模型探讨
  • 域1:安全与风险管理 第1章实现安全治理的原则和策略
  • 【大数据】学习大数据开发应该从哪些技术栈开始学习?
  • CentOS快速配置网络Docker快速部署
  • Grounded-SAM Demo部署搭建
  • C语言 | 第十六章 | 共用体 家庭收支软件-1
  • 【论文阅读】Learning a Few-shot Embedding Model with Contrastive Learning
  • OKHTTP 如何处理请求超时和重连机制
  • 基于Springboot vue的流浪狗领养管理系统设计与实现
  • 爬虫案例——网易新闻数据的爬取
  • SpringCloud 2023 Gateway的Filter配置介绍、类型、内置过滤器、自定义全局和单一内置过滤器
  • 从银幕到现实:擎天柱机器人即将改变我们的生活
  • 408算法题leetcode--第33天
  • OCR模型调研及详细安装
  • C++第六讲:STL--vector的使用及模拟实现
  • 2024年字节抖音前端面经,这次问的很基础!
  • vscode提交修改Failed to connect to github.com port 443: Timed out
  • 通过docker镜像安装elasticsearch和kibana