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

embedded/2024/10/19 10:11:44/

4.4 装饰模式

1.模式定义

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

2.模式结构

image-20241013161535947

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

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

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

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

3.模式原理

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

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

4.代码模板

抽象装饰类

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

具体装饰类

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

5.案例分析

image-20241013173014212

java">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.ppmy.cn/embedded/128713.html

相关文章

jsch ssh liunx秘钥交换失败

报错的堆栈信息 java.io.IOException: Key exchange was not finished, connection is closed.at ch.ethz.ssh2.transport.KexManager.getOrWaitForConnectionInfo(KexManager.java:75)at ch.ethz.ssh2.transport.TransportManager.getConnectionInfo(TransportManager.java:1…

关于希尔排序的理解

今天复习希尔排序的时候,对希尔排序有了新的理解 首先希尔排序是什么:希尔排序(Shell Sort)是一种基于插入排序的排序算法,又称缩小增量排序(Diminishing Increment Sort),是直接插…

结构体指针

结构体指针 作用&#xff1a;通过指针访问结构体中的成员。 利用操作符->可以通过结构体指针访问结构体属性。 struct student() {string name;int age;int score; } int main() {student s {"张三",18,100};struct *p &s;cout << "姓名&…

《环境感知:开启智能生活新视角》

《环境感知&#xff1a;开启智能生活新视角》 一、环境感知的定义与作用二、环境感知的技术与方法&#xff08;一&#xff09;传感器技术&#xff08;二&#xff09;数据融合技术&#xff08;三&#xff09;机器学习与深度学习技术 三、环境感知在不同领域的应用&#xff08;一…

【进阶OpenCV】 (14)-- 人脸识别 -- LBPH 算法

文章目录 LBPH 算法一、基本思想二、LBPH算法步骤1. 图像划分2. 局部二值模式特征提取3. 直方图统计4. 特征向量生成5. 相似度计算 三、代码实现1. 图像预处理2. 创建一个LBPH的人脸识别器3. 训练实例模型4. 图像预测 总结 LBPH 算法 **LBPH(Local Binary Patterns Histogram&…

[论文笔记] llama3.2 蒸馏

参考链接: LLaMA3.2技术报告: GitHub - meta-llama/llama-stack: Model components of the Llama Stack APIs [2407.21783] The Llama 3 Herd of Models https://ai.meta.com/blog/llama-3-2-connect-2024-vision-edge-mobile-devices/ HuggingFace:

D2000国产化加固笔记本电脑:筑牢信息安全防线

在这个数字化时代&#xff0c;信息技术的飞速发展不仅极大地丰富了我们的生活与工作方式&#xff0c;也悄然间将数据安全推向了前所未有的高度。近期&#xff0c;微软蓝屏事件再次敲响了全球用户心中的警钟&#xff0c;提醒我们&#xff0c;在享受技术便利的同时&#xff0c;电…

逍遥安卓模拟器命令行合集(memuc命令)

逍遥安卓模拟器命令行合集&#xff08;memuc命令&#xff09; 用cmd自行测试 模拟器配合工具&#xff1a;memuc是v6.0.0版本推出的命令行工具&#xff0c;它封装了MEmuConsole、MEmu、MEmuManage的接口&#xff0c;支持多开管理、修改配置、android通信、adb命令等功能。 memu…