public interface Shape {
void draw();
}
public class RectangleImpl implements Shape {
@Override
public void draw() {
System.out.println("画一个矩形");
}
}
现在我们想在原来的矩形上加一个边框、内阴影、外阴影,我们可以先创建一个装饰类的基类BaseDecorator
。
public abstract class BaseDecorator implements Shape {
protected final Shape shape;
public BaseDecorator(Shape shape) {
this.shape = shape;
}
}
然后分别实现几个装饰类,BorderDecorator
、InnerShadowDecorator
、OuterShadowDecorator
。
1public class BorderDecorator extends BaseDecorator {
2 public BorderDecorator(Shape shape) {
3 super(shape);
4 }
5 @Override
6 public void draw() {
7 System.out.println("画边框");
8 shape.draw();
9 }
10}
11
12public class InnerShadowDecorator extends BaseDecorator {
13 public InnerShadowDecorator(Shape shape) {
14 super(shape);
15 }
16 @Override
17 public void draw() {
18 System.out.println("画内阴影");
19 shape.draw();
20 }
21}
22
23public class OuterShadowDecorator extends BaseDecorator {
24 public OuterShadowDecorator(Shape shape) {
25 super(shape);
26 }
27 @Override
28 public void draw() {
29 System.out.println("画外阴影");
30 shape.draw();
31 }
32}
使用的时候可以根据需求进行组合,比如说要画一个带外阴影和边框的矩形,只需要一层层包装即可。
Shape origin = new Rectangle();
Shape outerShadow = new OuterShadowDecorator(origin);
Shape rectangle = new BorderDecorator(outerShadow);
使用装饰模式可以实现动态、组合扩展功能,但是如果装饰类太多,使用起来也非常麻烦,而且在实际开发中可能还需要考虑多个装饰类的顺序问题。