把书读薄 | 《设计模式之美》设计模式与范式(结构型-装饰器模式)(上)

简介: 本文对应设计模式与范式:结构型(50),装饰器模式 (Decorator Pattern)。装饰器模式和上节学的**桥接模式**(分离实体和行为) 代码结构非常相似,都是用组合来扩展原有类,但解决的问题大不相同。Tips:二手知识加工难免有所纰漏,感兴趣有时间的可自行查阅原文,谢谢。

0x1、定义


允许动态地向一个现有的对象添加新功能,同时不改变其结构,相当于对现有对象的进行了一个包装。


很好理解,就是套了一层,跟代理模式又不一样,装饰器模式可以套娃一样套多层。


0x2、写个例子


桥接模式广度装饰器模式深度,咋体现?还是上节形状的例子:


abstract class Shape {
    abstract void show();
}
// 形状
public class Circle extends Shape {
    @Override void show() { System.out.println("圆形"); }
}
public class Square extends Shape {
    @Override void show() { System.out.println("矩形"); }
}
// 引入颜色
public class RedCircle extends Shape {
    @Override void show() { System.out.println("红色圆形"); }
}
public class RedSquare extends Shape {
    @Override void show() { System.out.println("红色矩形"); }
}
public class BlueCircle extends Shape {
    @Override void show() { System.out.println("蓝色圆形"); }
}
public class BlueSquare extends Shape {
    @Override void show() { System.out.println("蓝色矩形"); }
}


上节是形状或者颜色变多,现在不是,是需求开始变多,比如添加 质感:磨砂和光滑


public class SmoothRedCircle extends RedCircle      // 光滑红色圆形
public class SmoothRedSquare extends RedSquare      // 光滑红色矩形
public class SmoothBlueCircle extends BlueCircle    // 光滑蓝色圆形
public class SmoothBlueSquare extends BlueSquare    // 光滑蓝色矩形
public class MatteRedCircle extends RedCircle       // 磨砂红色圆形
public class MatteRedSquare extends RedSquare       // 磨砂红色矩形
public class MatteBlueCircle extends BlueCircle     // 磨砂蓝色圆形
public class MatteBlueSquare extends BlueSquare     // 磨砂蓝色矩形


可以,子类变成8个了,接着添加 大小:大中小,子类会变成8*3=24个,再加?直接类爆炸~


这种深度多层继承的场景,用装饰器模式就很适合了:


// 抽象组件(接口和抽象类都可以)
interface IShape {
    String show();
}
// 抽象装饰类(内部有一个指向组件对象的引用,用来调装饰前对象的方法)
public abstract class BaseDecorator implements IShape {
    private IShape shape;
    public BaseDecorator(IShape shape) { this.shape = shape; }
    @Override public String show() { return shape.show(); }
}
// 具体组件类
public class CircleShape implements IShape {
    @Override public String show() { return "圆形"; }
}
public class SquareShape implements IShape {
    @Override public String show() { return "矩形"; }
}
// 颜色具体装饰类(可调用抽象装饰类中定义的方法,也可新增方法来扩展对象行为)
public class RedDecorator extends BaseDecorator {
    public RedDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "红色" + super.show(); }
}
public class BlueDecorator extends BaseDecorator {
    public BlueDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "蓝色" + super.show(); }
}
// 材质具体装饰类
public class SmoothDecorator extends BaseDecorator {
    public SmoothDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "光滑" + super.show(); }
}
public class MatteDecorator extends BaseDecorator {
    public MatteDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "磨砂" + super.show(); }
}
// 大小具体装饰类
public class BigDecorator extends BaseDecorator {
    public BigDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "大" + super.show(); }
}
public class MiddleDecorator extends BaseDecorator {
    public MiddleDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "中" + super.show(); }
}
public class SmallDecorator extends BaseDecorator {
    public SmallDecorator(IShape shape) { super(shape); }
    @Override public String show() { return "小" + super.show(); }
}
// 测试用例
public class DecoratorTest {
    public static void main(String[] args) {
        IShape circle = new CircleShape();
        IShape square = new SquareShape();
        IShape redCircle =  new RedDecorator(circle);
        IShape smoothBlueSquare = new SmoothDecorator(new BlueDecorator(square));
        IShape bigMatteRedCircle = new BigDecorator(new MatteDecorator(redCircle));
        System.out.println(circle.show());
        System.out.println(square.show());
        System.out.println(redCircle.show());
        System.out.println(smoothBlueSquare.show());
        System.out.println(bigMatteRedCircle.show());
    }
}


运行输出结果如下


网络异常,图片无法展示
|


用继承要24个子类,用装饰器模式只要10个,顺带画出UML类图:


网络异常,图片无法展示
|

相关文章
|
3月前
|
设计模式 存储 缓存
聊聊Java设计模式-装饰器模式
装饰器模式允许向一个现有的对象添加新的功能,同时不改变其结果。比如Java 中的IO框架中,`FileInputStream`(处理文件)、`ByteArrayInputStream`(处理字节数组)、`BufferedInputStream`(带缓存的处理类)等就是对`InputStream`进行的功能扩展,这就是装饰器模式的典型应用。
25 1
聊聊Java设计模式-装饰器模式
|
3月前
|
设计模式 Java
常用设计模式(工厂方法,抽象工厂,责任链,装饰器模式)
有关设计模式的其他常用模式请参考 单例模式的实现 常见的设计模式(模板与方法,观察者模式,策略模式)
39 2
|
2天前
|
设计模式 Go 网络安全
[设计模式 Go实现] 结构型~代理模式
[设计模式 Go实现] 结构型~代理模式
|
2天前
|
设计模式 Go
[设计模式 Go实现] 结构型~装饰模式
[设计模式 Go实现] 结构型~装饰模式
|
2天前
|
设计模式 Go
[设计模式 Go实现] 结构型~适配器模式
[设计模式 Go实现] 结构型~适配器模式
|
1月前
|
设计模式 Java
设计模式之装饰器模式
设计模式之装饰器模式
|
3月前
|
设计模式 Java uml
设计模式-装饰器模式
设计模式-装饰器模式
23 0
|
3月前
|
设计模式
结构型设计模式:装饰器模式
结构型设计模式:装饰器模式
22 0
|
3月前
|
设计模式 缓存 测试技术
python的装饰器与设计模式中的装饰器模式
python的装饰器与设计模式中的装饰器模式
|
17天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0