设计模式 - 装饰器模式

装饰器模式(包装器: 功能增强):动态的给一个对象添加一些额外的职责(是继承的替代,但相比继承而言,组合的方式更具灵活性)
实现方式:(组合)装饰类(Decorator) + 被装饰接口(IComponent) - 动态地对Component对象的功能进行增强,避免了继承的弊端(尤其是多重继承)
核心:{多重}组合 + 功能增强 - 继承是静态添加功能,而装饰器模式是通过组合动态地添加功能(可选择地装配,可卸载)
角色:

  • IComponent 抽象构件接口:核心对象,具体构件和装饰角色均需继承自该对象
  • Component 具体构件:被装饰地类。
  • IDecorator 装饰角色接口:通过组合地方式持有一个private引用,指向Component抽象构件(多重装饰地情况下,Decorator对象同时也作为中间过程地Component对象)
  • DecoratorX 具体装饰器角色:实现功能增强的装饰代码。

Test

// 装饰器模式,不侵入被装饰对象,动态地实现功能增强(选择装配 + 可拆卸)
public class Test {
    public static void main(String[] args) {
        IComponent componentA = new Component();
        LineReadDecorator lineReadDecorator = new LineReadDecorator(componentA);
        lineReadDecorator.read();
        // 输出:read bytes by line!

        IComponent componentB = new Component();
        BufferedReadDecorator bufferedReadDecorator = new BufferedReadDecorator(componentB);
        StringReadDecorator stringReadDecorator = new StringReadDecorator(bufferedReadDecorator);
        stringReadDecorator.read();
        // 输出:read bytes with buffered by String!
    }
}

Decorator

// Decorator和Component属于同一继承体系,并持有Component对象的引用(增强过程中需要调用component对象的方法)
// 抽象的Decorator作为基本实现,不添加额外的增强,增强由子类实现。
public abstract class IDecorator extends IComponent{
    private IComponent component = null;

    // 构造方法参数类型为IComponent,即:(多重包装的情况下)IDecorator类型将被作为IComponent被传入
    protected IDecorator(IComponent component){
        this.component = component;
    }

    @Override
    void read() {
        component.read();
    }
}
public class BufferedReadDecorator extends IDecorator{
    public BufferedReadDecorator(AbstractComponent component) {
        super(component);
    }

    // 装饰器实现类,通过IComponent的业务方法以及额外的逻辑,实现功能增强
    @Override
    void read() {
        super.read();
        System.out.print("with buffered ");
    }
}

Component

// IComponent 对象无需知晓装饰器的存在。
public abstract class IComponent {
    abstract void read();
}

public class Component extends AbstractComponent{
    @Override
    void read() {
        System.out.print("read bytes ");
    }
}
posted @ 2020-11-27 20:29  祁奇  阅读(60)  评论(0编辑  收藏  举报