Decorator模式
|
|-定义:在不改变原有对象的基础之上,将功能附加到对象上。(向被装饰对象添加某些功能)
|
|-适用场景 |--1.扩展一个类的功能或者给一个类添加附加职责
| |--2.给一个对象动态的添加功能,或动态撤销功能
|
|-涉及的类 |--装饰类和被装饰类共同的接口或基类
| |
| |--被装饰类
| |
| |--抽象装饰类
| |
| |--提供装饰的类
|
|-java.io.InputStream为例|--基类 : InputStream
| |--被装饰类 : FileInputStream,ByteArrayInputStream
| |--抽象装饰类 : FilterInputStream
| |--具体装饰类 : BufferedInputStream,LineInputStream,...
装饰者模式类图
________________
| Component |
|----------------|
|----------------|
|+ operate():void|
|________________|
/|\ /|\
/ \
__________________/ \ ______________________
| ConcreteComponent| | Decorator |
|------------------| |----------------------|
|------------------| |- conponent:Component |
|+ operate():void | |----------------------|
|__________________| |+ Decorator(Component)|
|+ operate():void |
|______________________|
/|\ /|\
/ \
_____________________ / \ _____________________
| ConcreteDecoratorA | | ConcreteDecoratorB |
|---------------------| |---------------------|
|- conponent:Component| |- conponent:Component|
|---------------------| |---------------------|
|+ operate():void | |+ operate():void |
|+ newMethodA():void | |+ newMethodB():void |
|_____________________| |_____________________|