[工作中的设计模式]装饰模式decorator

时间:2021-12-10 16:36:34

一、模式解析

 装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

装饰模式的要点主要是:

1、需要对已有对象扩展新的功能,又不希望改变原有对象接口;

2、装饰者对象与原有对象需要继承相同接口,初始化装饰对象时将原有对象传入装饰对象;

3、可以对一个对象定义多个装饰着对象,进行分别装饰或者组合装饰

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnQAAAFzCAIAAAA45gO9AAAgAElEQVR4nO2doZOkyPO3W03EuZEn13TEypEjECvP4k5OBObkVyDOjSLuHyDi5AjEyV+gTiLPEHFyA7WOlScQI3lF7uZbW9AMPXRDFv08YqKbqYYkK8kPVRRVhx4AAAAuymFrAwAAAPYG4goAAHBhEFcAAIALg7gCAABcGMQVAADgwiCuAAAAFwZxBQAAuDCIKwAAwIVBXAEAAC4M4goAAHBhEFcAAIALg7gCAABcGMQVAADgwiCuAAAAFwZxBQAAuDCIKwAAwIVBXAEAAC4M4goAAHBhEFcAAIALg7gCAABcGMQVAADgwiCuAAAAFwZxBQAAuDCIKwAAwIVBXAEAAC4M4goAAHBhEFcAAIALg7gCAABcGFvi+vDwcAAAuDEeHh62zr5wYWyJ6+Fgyx4AgBUg9e0PWzVKhAHADULq2x+2apQIA4AbhNS3P2zVKBEGADcIqW9/2KpRIuzGKcuyaRpvY13XZVm6ZaIoWseePM/LssyybGjVOyjLsq7rORvh1iD17Q9bNUqE3TKjMuMpq5Dn+ZwdLlTEoijk0GVZzjzim6CvMAqpb3/YqlEi7GaZr6zzSdN0iUlZll1D89BXGELq2x+2apQIu03mK2tVVWmaxnHsbkmSpCzLNE1VTZumyfP8eDyWZVmWZdd1Wj7P8zRNtSUqJWWHRVHEcdy2bdd1ZVkmSSLdwtoCbpomyzL35+4+syzzTko2DhvQ6Ct4kPr2h60aJcJukLIsq6ryNrZte6ontq5r75nr8XgUWfL6b10NFtI0lZJd17n/jaJIf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f6QsJMY+Ys4Dy83T6XCXuu9EpfvcoK0qMTCyx0/hCz4ioQqwshVq+BFTtgTa4Xf9I4uMYDmyu9QX9V7KwgvRDj4vo+iFUXYvXiWLED1sRO/EEQ7FJcYZfYCRgrdsCa2Ik/CALEFULBTsBYsQPWxE78QRAgrhAKdgLGih2wJnbiD4IAcYVQsBMwVuyANbm/vz8AzOb+/p5YhSDYMFY9EFcAAIALg7gCAABcGMQVAADgwiCuAAAAFwZxBQAAuDCIKwAAwIVBXAEAAC4M4goAAHBhEFcAAIALg7gCAABcGMQVAADgwiCuAAAAFwZxBQAAuDCIKwAAwIVBXAEAAC4M4goAAHBhEFcAAIALg7gCAABcGMQVAADgwiCuAAAAFwZxBQAAuDCIKwAAwIVBXAEAAC4M4goAAHBhEFcAAIALg7gCAABcmP8HDeRDlKf1KVUAAAAASUVORK5CYII=" alt="" width="628" height="371" />

二、模式代码

1、抽象接口

package decorator.patten;

public interface Component {
public void operation();
}

2、定义被装饰对象

package decorator.patten;

public class ConcreteComponent implements Component {

    @Override
public void operation() {
System.out.println("我是被装饰对象,我执行了");
} }

3、定义装饰者对象A

package decorator.patten;

public class DecoratorA implements Component {
Component component;
public DecoratorA(Component component){
this.component=component;
}
@Override
public void operation() {
System.out.println("我是装饰对象A,我在被装饰对象前增加打印");
component.operation();
} }

4、定义装饰者对象B

package decorator.patten;

public class DecoratorB implements Component {
Component component;
public DecoratorB(Component component){
this.component=component;
}
@Override
public void operation() {
component.operation();
System.out.println("我是装饰对象b,我在被装饰对象后添加打印");
} }

5、定义客户端

public class Client {
public static void main(String[] args) {
Component component=new DecoratorB(new DecoratorA(new ConcreteComponent()));
component.operation();
}
}

6、执行结果

我是装饰对象A,我在被装饰对象前增加打印
我是被装饰对象,我执行了
我是装饰对象b,我在被装饰对象后添加打印

三、说明

1、装饰着模式可以为被装饰者添加新的功能,,将核心功能和装饰功能进行分离,比如日志打印,字符集处理等
2、可以对对象进行多此装饰,装饰均会被执行
3、示例中多次装饰没有顺序,但实际中往往会是有序的,比如数据加密和数据过滤,如果先加密再过滤就会出现问题
4、装饰对象和被装饰对象均集成同一个接口,有时候为了简化,我们会将装饰对象直接集成被装饰对象,这就是子类重写父类方法达到扩展功能的
四、应用场景

装饰模式最典型的应用就是java IO中对inputstream和outputstream的装饰,例如

dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("test.txt")
)
);