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类图:
网络异常,图片无法展示
|