装饰器模式(Decorator Pattern)在不改变原有类的结构的情况下,对现有的对象添加新的功能。
一、需求举例:
几何中的图形(Shape)的子类有长方形(Rectangle),三角形(Triangle)和圆形(Circular);
颜色类:红色(Red),黄色(Yellow),蓝色(Blue)。
通过这些类,在不改变原有图形类的情况下,输出红色长方形,黄色三角形和蓝色圆形。
二、类图:
三、代码实例:
1.图形类
1.1 图形父类:IShape
package com.lance.decorator.shape;
public interface IShape {
default void drawShape() {
System.out.println("draw default shape.");
}
}
1.2 圆形类:Circle
package com.lance.decorator.shape;
public class Circle implements IShape {
@Override
public void drawShape() {
System.out.println("draw Circle.");
}
}
1.3 三角形类:Triangle
package com.lance.decorator.shape;
public class Triangle implements IShape {
@Override
public void drawShape() {
System.out.println("draw Triangle.");
}
}
1.4 长方形类:
package com.lance.decorator.shape;
public class Rectangle implements IShape {
@Override
public void drawShape() {
System.out.println("draw Rectangle.");
}
}
2.颜色装饰类:
2.1 颜色装饰父类:ColorDecorator
package com.lance.decorator.decorator;
import com.lance.decorator.shape.IShape;
public abstract class ColorDecorator implements IShape {
protected IShape decoratedShape;
public ColorDecorator(IShape decoratedShape) {
this.decoratedShape = decoratedShape;
}
@Override
public void drawShape() {
decoratedShape.drawShape();
}
}
2.2 红色装饰类:RedDecorator
package com.lance.decorator.decorator;
import com.lance.decorator.shape.IShape;
public class RedDecorator extends ColorDecorator {
public RedDecorator(IShape decoratedShape) {
super(decoratedShape);
}
@Override
public void drawShape() {
decoratedShape.drawShape();
System.out.println("the color of shape is red.");
}
}
2.3 黄色装饰类:YellowDecorator
package com.lance.decorator.decorator;
import com.lance.decorator.shape.IShape;
public class YellowDecorator extends ColorDecorator {
public YellowDecorator(IShape decoratedShape) {
super(decoratedShape);
}
@Override
public void drawShape() {
decoratedShape.drawShape();
System.out.println("the color of shape is yellow.");
}
}
2.4 蓝色装饰类:BlueDecorator
package com.lance.decorator.decorator;
import com.lance.decorator.shape.IShape;
public class BlueDecorator extends ColorDecorator {
public BlueDecorator(IShape decoratedShape) {
super(decoratedShape);
}
@Override
public void drawShape() {
decoratedShape.drawShape();
System.out.println("the color of shape is blue.");
}
}
3.主类:DecoratorPattern
package com.lance.decorator;
import com.lance.decorator.decorator.BlueDecorator;
import com.lance.decorator.decorator.RedDecorator;
import com.lance.decorator.decorator.YellowDecorator;
import com.lance.decorator.shape.Circle;
import com.lance.decorator.shape.IShape;
import com.lance.decorator.shape.Rectangle;
import com.lance.decorator.shape.Triangle;
public class DecoratorPattern {
public static void main(String[] args) {
System.out.println("==========start==========");
IShape redRectangle = new RedDecorator(new Rectangle());
redRectangle.drawShape();
System.out.println("=======================");
IShape yellowTriangle = new YellowDecorator(new Triangle());
yellowTriangle.drawShape();
System.out.println("=======================");
IShape blueCircle = new BlueDecorator(new Circle());
blueCircle.drawShape();
System.out.println("==========end==========");
}
}
输出结果:
==========start==========
draw Rectangle.
the color of shape is red.
=======================
draw Triangle.
the color of shape is yellow.
=======================
draw Circle.
the color of shape is blue.
==========end==========