2. 结构型模式 (2) 装饰者模式

阅读更多
2. 结构型模式 (2) 装饰器模式

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。

动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。

在不想增加很多子类的情况下扩展类。一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。

1. Component 类充当抽象角色,不应该具体实现。
2. 修饰类引用和继承 Component 类,具体扩展类重写父类方法。


1. 装饰器模式

package com.andrew.pattern0202.decorator.modal01;
public interface Shape {
    void draw();
}

package com.andrew.pattern0202.decorator.modal01;
public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Shape: Rectangle");
    }
}

package com.andrew.pattern0202.decorator.modal01;
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Shape: Circle");
    }
}


package com.andrew.pattern0202.decorator.modal01;
public class ShapeDecorator implements Shape {
    protected Shape decoratedShape;
    public ShapeDecorator(Shape decoratedShape){
        this.decoratedShape = decoratedShape;
    }
    @Override
    public void draw() {
        decoratedShape.draw();
    }
}


package com.andrew.pattern0202.decorator.modal01;
public 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");
    }
}


package com.andrew.pattern0202.decorator.modal01;
/**
 * 1. 装饰者模式
 * 
 * @author andrew
 * @date 2017/06/26
 */
public class DecoratorTest {
    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("Circle of red border");
        redCircle.draw();
        System.out.println("Rectangle of red border");
        redRectangle.draw();
    }
}
运行结果:
Circle with normal border
Shape: Circle
Circle of red border
Shape: Circle
Border Color: Red
Rectangle of red border
Shape: Rectangle
Border Color: Red


2. 装饰器模式
package com.andrew.pattern0202.decorator.modal02;
public interface Component {
    public void doSomething();
}

package com.andrew.pattern0202.decorator.modal02;
public class ConcreteComponent implements Component {
    @Override
    public void doSomething() {
        System.out.println("基本功能");
    }
}


package com.andrew.pattern0202.decorator.modal02;
public class Decorator implements Component {
    private Component component;
    public Decorator(Component component) {
        this.component = component;
    }
    @Override
    public void doSomething() {
        component.doSomething();
    }
}


package com.andrew.pattern0202.decorator.modal02;
public class ConcreteDecorator1 extends Decorator {
    public ConcreteDecorator1(Component component) {
        super(component);
    }
    @Override
    public void doSomething() {
        super.doSomething();
        this.doAnotherThing();
    }
    private void doAnotherThing() {
        System.out.println("附加功能1");
    }
}

package com.andrew.pattern0202.decorator.modal02;
public class ConcreteDecorator2 extends Decorator {
    public ConcreteDecorator2(Component component) {
        super(component);
    }
    @Override
    public void doSomething() {
        super.doSomething();
        this.doAnotherThing();
    }
    private void doAnotherThing() {
        System.out.println("附加功能2");
    }
}


package com.andrew.pattern0202.decorator.modal02;
/**
 * 2. 装饰者模式
 * 
 * @author nxf46916
 * @date 2017/06/26
 */
public class Client {
    public static void main(String[] args) {
        // 节点流
        Component component = new ConcreteComponent();
        // 过滤流
        Component component2 = new ConcreteDecorator1(component);
        component2.doSomething();
        System.out.println("------------");
        // 过滤流
        Component component3 = new ConcreteDecorator2(component2);
        component3.doSomething();
        System.out.println("------------");
        Component component11 = new ConcreteDecorator1(new ConcreteDecorator2(new ConcreteComponent()));
        component11.doSomething();
    }
}
运行结果:
基本功能
附加功能1
------------
基本功能
附加功能1
附加功能2
------------
基本功能
附加功能2
附加功能1

你可能感兴趣的:(java,设计模式)