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

四、装饰者模式

一、模式定义

  在不改变原有对象的前提下(不改变ConcreteComponent情况下),给原有对象扩展功能(利用ConcreteDecorator扩展功能,在ConcreteDecorator中注入ConcreteComponent)。如下所示:
clipboard

二、应用场景

  扩展一个类的功能,或者给一个类添加一些附加职责

三、优点:

①、灵活改变组合;
②、符合开闭原则;

四、装饰者模式的实现方式

4.1、方式1
package decorator;public class DecoratorDesignPattern {public static void main(String[] args) {Decorator decoratorA = new ConcreteDecoratorA(new ConcreteComponent());decoratorA.operation();Decorator decoratorB = new ConcreteDecoratorB(new ConcreteDecoratorA(new ConcreteComponent()));decoratorB.operation();}
}interface Component{void operation();
}class ConcreteComponent implements Component{@Overridepublic void operation() {System.out.println("拍照...\n");}
}abstract class Decorator implements Component{Component component;public Decorator(Component component) {this.component = component;}
}class ConcreteDecoratorA extends Decorator{public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {System.out.println("添加美颜....");component.operation();}
}class ConcreteDecoratorB extends Decorator{public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void operation() {System.out.println("添加滤镜");component.operation();}
}

上面代码的运行结果如下:
clipboard

4.2、方式2
package decorator;public class DecoratorPattern {public static void main(String[] args) {Circle circle = new Circle();RedShapeDecorator redCircle = new RedShapeDecorator(circle);Rectangle rectangle = new Rectangle();RedShapeDecorator redRectangle = new RedShapeDecorator(rectangle);System.out.println("Circle with normal border");circle.draw();System.out.println("\nCircle of red border");redCircle.draw();System.out.println("\nRectangle of red border");redRectangle.draw();}
}interface Shape {void draw();
}class Circle implements Shape {@Overridepublic void draw() {System.out.println("Shape:Circle");}
}class Rectangle implements Shape {@Overridepublic void draw() {System.out.println("Shape:Rectangle");}
}abstract class ShapeDecorator implements Shape {public Shape decoratedShape;public ShapeDecorator(Shape decoratorShape) {this.decoratedShape = decoratorShape;}@Overridepublic void draw() {decoratedShape.draw();}
}class RedShapeDecorator extends ShapeDecorator {public RedShapeDecorator(Shape decoratorShape) {super(decoratorShape);}@Overridepublic void draw() {decoratedShape.draw();setRedBorder(decoratedShape);}private void setRedBorder(Shape decoratedShape){System.out.println("Border Color: Red");}
}

上面代码的运行结果如下:
clipboard

五、装饰者模式的应用

  Java IO 库采用了装饰器模式(Decorator Pattern)和适配器模式(Adapter Pattern)的组合设计模式,其中InputStream是装饰器模式中顶层的抽象类,FilterInputStream是装饰器基类,BufferedInputStream是带有缓冲区的装饰器类,ObjectInputStream是可以读取对象的装饰器类,SequenceInputStream是可以顺序读取多个输入Stream的装饰器类,FileInputStream、ByteArrayInputStream则是被装饰的类。 这些类的UML图,如下所示:
image

http://www.jsqmd.com/news/371385/

相关文章:

  • Jakarta EE开发中,如何配置IntelliJ IDEA的远程调试? - 实践
  • SQL中的LAST()函数详解
  • 简单题 2
  • 7个AI降重神器,轻松搞定论文查重
  • The Jam/MR Executable Program
  • 科研人福利:AI降重工具Top7盘点
  • 学术党必看!AI降重工具排名榜单
  • 从视频学会折纸?ByteDance团队让AI首次通过看视频掌握复杂技能
  • 数据安全
  • AI提示工程云端部署方案对比:Serverless vs K8s vs 虚拟机(适用场景分析)
  • 北大团队发布Chain of Mindset:让AI灵活切换思维模式的推理框架
  • 耶鲁大学团队如何让电脑助手学会“看懂“桌面操作
  • 7大AI降重工具测评,提升论文通过率
  • 《GraphQL批处理与全局缓存共享的底层逻辑》
  • 学术AI工具盘点:10个论文写作网站详解
  • 完整教程:Spring Boot 多数据源解决方案:dynamic-datasource-spring-boot-starter 的奥秘(上)
  • 《GraphQL状态图建模与低时延控制能力解析》
  • 论文写作AI工具:10款网站功能全解析
  • IPV6安全保护
  • jam
  • 2025.2.9 做题记录
  • linux三剑客基础入门
  • Kubernetes部署Cilium网络插件命令 - wanghongwei
  • 肯尼斯·费雷尔的价值因子研究
  • 【YOLOv12多模态涨点改进】独家创新首发| CVPR 2025 | 引入FDSM频率域动态地选择模块,高效融合红外和可见光多模态特征,精准保留有用信息、抑制冗余与噪声,助力目标检测、图像分割、分类
  • 提示工程架构师实战教程:群体智能提示优化方法论在金融领域应用
  • 【YOLOv12多模态涨点改进】CVPR 2025 | 引入RLAB残差线性注意力块,有效融合并强调多尺度特征,多种创新改进点,助力多模态融合目标检测、图像分割、图像分类,医学图像分割等任务有效涨点
  • Redis在大数据日志处理中的应用:ELK+Redis架构解析
  • 4个领先的大模型微调工具
  • 基于大数据的物联网预测性维护系统设计