package com.interview.design.pattern.structural;
/**
* Created_By: stefanie
* Date: 14-12-3
* Time: 上午11:07
* 装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例
*
* Decorator pattern allows to add new functionality an existing object without altering its structure.
* This pattern creates a decorator class which wraps the original class and provides additional functionality
* keeping class methods signature intact.
*
* 装饰器模式的应用场景:
* 1、需要扩展一个类的功能。
* 2、动态的为一个对象增加功能,而且还能动态撤销。(继承不能做到这一点,继承的功能是静态的,不能动态增删。)
* 缺点:产生过多相似的对象,不易排错!
*/
public class DecoratorPattern {
static interface Shape {
void draw();
}
static class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Rectangle");
}
}
static class Circle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Circle");
}
}
/**
* use ShapeDecorator to add more logic in draw() for Shape, and it composite a Shape instance
*/
static abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){
decoratedShape.draw();
}
}
/**
* Add more logic when call draw()
*/
static class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
}
public static void main(String[] args) {
Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(new Circle());
Shape redRectangle = new RedShapeDecorator(new Rectangle());
System.out.println("Circle with normal border");
circle.draw();
System.out.println("\nCircle of red border");
redCircle.draw();
System.out.println("\nRectangle of red border");
redRectangle.draw();
}
}