Java结构型设计模式之装饰模式详解
作者:别偷我的猪_09 时间:2022-07-13 10:00:35
介绍
意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更灵活。
主要解决:我们扩展一个类常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会越来越膨胀。
如何使用:在不想增加很多子类的情况下扩展。
如何解决:将具体功能职责划分,同时继承装饰者模式。
关键代码:
1. Component 类充当抽象角色,不应该具体实现。
2. 修饰类引用合继承 Component 类,具体扩展类重写父类方法。
使用场景:
1. 扩展一个类的功能。
2. 动态增加功能,动态撤销。
实现
创建一个 Shape 接口合实现了 Shape 接口的实体类。然后再创建一个实现了 Shape 接口的抽象装饰类 ShapeDecorator,并把 Shape 对象作为它的实例变量。RedShapeDecorator 是实现了 ShapeDecorator 的实体类。DecoratorPatternDemo 类使用 RedShapeDecorator 来装饰 Shape 对象。
步骤1:
public interface Shape{
void draw();
}
步骤2:
public class Rectangle implements Shape{
@Override
public ovid draw(){
System.out.println("draw Rectangle");
}
}
public class Cricle implements Shape{
@Override
public ovid draw(){
System.out.println("draw Circle");
}
}
步骤3:
public abstract class ShapeDecorator implements Shape{
private Shape shape; // 持有一个 Shape 对象
public ShapeDecorator(Shape shape){
this.shape = shape;
}
public void draw(){
shape.draw();// TODO 根据传进来的具体 Shape 对象,调用对应的 draw 方法
}
}
步骤4:
public class RedShapeDecorator extends ShapeDecorator{
public RedShapeDecorator(Shape shape){
super(shape);
}
@Override
public void draw(){
shape.draw();
setRedBorder(shape);
}
public void setRedBorder(Shape shape){
System.out.println("Border Color: Red");
}
}
步骤5
public class DecoratorPatterndDemo{
public static void main(String args[]){
//TODO 面向抽象层编程
// 普通的Circle
Shape circle = new Circle();
System.out.println("Circle with normal Border");
circle.darw();
// 红色边界的 Circle
ShapeDecorator redCircle = new RedShapeDecorator(new Circle);
System.out.println("Circle of red Border");
redCircle.draw();
// 红色边界的 Rectangle
ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangel):
System.out.println("Rectangle of red Border");
redRectangle.draw();
}
}
优缺点
优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
缺点:多层装饰比较复杂。
来源:https://blog.csdn.net/qq_44950283/article/details/129829659
标签:Java,设计模式,装饰模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#基于Sockets类实现TCP通讯
2023-03-22 05:08:30
![](https://img.aspxhome.com/file/2023/9/111189_0s.jpg)
Idea 搭建Spring源码环境的超详细教程
2023-09-30 16:13:17
![](https://img.aspxhome.com/file/2023/9/132059_0s.jpg)
关于C#连接SQL Server时提示用户登录失败的解决方法
2021-10-03 21:21:58
![](https://img.aspxhome.com/file/2023/1/79071_0s.png)
详解Android中Drawable方法
2021-06-12 12:40:05
Android仿最新微信相机功能
2022-06-08 00:35:08
![](https://img.aspxhome.com/file/2023/7/137257_0s.jpg)
java中ConcurrentHashMap的读操作为什么不需要加锁
2021-10-07 18:30:44
![](https://img.aspxhome.com/file/2023/1/132181_0s.png)
RocketMQ设计之主从复制和读写分离
2022-08-03 15:55:14
使用Spring Cloud Feign上传文件的示例
2021-08-25 04:37:38
spring 整合mybatis后用不上session缓存的原因分析
2021-12-09 10:11:03
spring aop注解配置代码实例
2022-02-16 21:13:33
java多线程模拟抢红包功能
2023-07-25 01:09:58
java生成随机字符串的两种方法
2022-09-09 22:49:26
AJAX中Get请求报错404的原因以及解决办法
2021-07-03 05:41:07
![](https://img.aspxhome.com/file/2023/2/67442_0s.jpg)
Spring Cloud Config对特殊字符加密处理的方法详解
2021-11-22 15:16:48
Spring Security 中细化权限粒度的方法
2022-08-30 14:26:40
![](https://img.aspxhome.com/file/2023/8/83498_0s.png)
C#中的高效IO库System.IO.Pipelines
2022-11-16 03:43:06
如何使用JCTools实现Java并发程序
2023-11-21 07:37:39
Android如何通过手机自动获取短信验证码
2022-12-10 00:19:47
![](https://img.aspxhome.com/file/2023/6/106816_0s.png)
Java多线程之Worker Thread模式
2021-06-11 12:11:51
Mybatis查询多条记录并返回List集合的方法
2023-08-08 05:16:48