装饰者模式目标设计模式
把许多要实现的功能,加载在子类上,类的继承,显得很臃肿,装饰着模式是在不改变原有类文件和使用继承的状况下,经过建立一个包装对象动态地扩展一个对象的功能,相比生成子类更为灵活ide
装饰模式的结构性能
在装饰模式中的角色有:this
● 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。spa
● 具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。设计
● 装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。3d
● 具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任code
源代码
抽象构件角色 component
[Java]纯文本查看__复制代码对象
1
2
3
4
5
public
interface
Component {
public
void
sampleOperation();
}
具体构件角色
[Java]纯文本查看__复制代码
1
2
3
4
5
6
7
8
public
class
ConcreteComponent
implements
Component {
@Override
public
void
sampleOperation() {
// 写相关的业务代码
}
}
装饰角色
[Java]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
public
class
Decorator
implements
Component{
private
Component component;
public
Decorator(Component component){
this
`.component = component;`
}
@Override
public
void
sampleOperation() {
// 委派给构件
component.sampleOperation();
}
}
具体装饰角色
[Java]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
public
class
ConcreteDecoratorA
extends
Decorator {
public
ConcreteDecoratorA(Component component) {
super
`(component);`
}
@Override
public
void
sampleOperation() {
super
`.sampleOperation();`
// 写相关的业务代码
}
}
[Java]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
public
class
ConcreteDecoratorB
extends
Decorator {
public
ConcreteDecoratorB(Component component) {
super
`(component);`
}
@Override
public
void
sampleOperation() {
super
`.sampleOperation();`
// 写相关的业务代码
}
}
齐天大圣的例子
孙悟空有七十二般变化,他的每一种变化都给他带来一种附加的本领。他变成鱼儿时,就能够到水里游泳;他变成鸟儿时,就能够在天上飞行。
本例中,Component的角色便由鼎鼎大名的齐天大圣扮演;ConcreteComponent的角色属于大圣的本尊,就是猢狲本人;Decorator的角色由大圣的七十二变扮演。而ConcreteDecorator的角色即是鱼儿、鸟儿等七十二般变化
源代码
抽象构件角色“齐天大圣”接口定义了一个move()方法,这是全部的具体构件类和装饰类必须实现的。
[Java]纯文本查看__复制代码
1
2
3
4
5
//大圣的尊号
public
interface
TheGreatestSage {
public
void
move();
}
具体构件角色“大圣本尊”猢狲类
[Java]纯文本查看__复制代码
1
2
3
4
5
6
7
8
9
public
class
Monkey
implements
TheGreatestSage {
@Override
public
void
move() {
//代码
System.out.println(
`"Monkey Move"`);
}
}
抽象装饰角色“七十二变”
[AppleScript]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
public
class
Change implements TheGreatestSage
{
private TheGreatestSage sage;
public Change
`(TheGreatestSage sage
)`{
this.sage
=
sage;
}
@Override
public void
move
`(`)
{
/
`/
代码`
sage.
`move(
)`;
}
}
具体装饰角色“鱼儿”
[Java]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
public
class
Fish
extends
Change {
public
Fish(TheGreatestSage sage) {
super
`(sage);`
}
@Override
public
void
move() {
// 代码
System.out.println(
`"Fish Move"`);
}
}
具体装饰角色“鸟儿”
[Java]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
public
class
Bird
extends
Change {
public
Bird(TheGreatestSage sage) {
super
`(sage);`
}
@Override
public
void
move() {
// 代码
System.out.println(
`"Bird Move"`);
}
}
客户端类
[Java]纯文本查看__复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
public
class
Client {
public
static
void
main(String[] args) {
TheGreatestSage sage =
new
Monkey();
// 第一种写法
TheGreatestSage bird =
new
Bird(sage);
TheGreatestSage fish =
new
Fish(bird);
// 第二种写法
//TheGreatestSage fish = new Fish(new Bird(sage));
fish.move();
}
}
“大圣本尊”是ConcreteComponent类,而“鸟儿”、“鱼儿”是装饰类。要装饰的是“大圣本尊”,也即“猢狲”实例。
上面的例子中,系统把大圣从一只猢狲装饰成了一只鸟儿(把鸟儿的功能加到了猢狲身上),而后又把鸟儿装饰成了一条鱼儿(把鱼儿的功能加到了猢狲+鸟儿身上,获得了猢狲+鸟儿+鱼儿)。
如上图所示,大圣的变化首先将鸟儿的功能附加到了猢狲身上,而后又将鱼儿的功能附加到猢狲+鸟儿身上。
设计模式在JAVA I/O库中的应用
装饰模式在Java语言中的最著名的应用莫过于Java I/O标准库的设计了。
因为Java I/O库须要不少性能的各类组合,若是这些性能都是用继承的方法实现的,那么每一种组合都须要一个类,这样就会形成大量性能重复的类出现。而若是采用装饰模式,那么类的数目就会大大减小,性能的重复也能够减至最少。所以装饰模式是Java I/O库的基本模式。
Java I/O库的对象结构图以下,因为Java I/O的对象众多,所以只画出InputStream的部分。
根据上图能够看出:
● 抽象构件(Component)角色:由InputStream扮演。这是一个抽象类,为各类子类型提供统一的接口。
● 具体构件(ConcreteComponent)角色:由ByteArrayInputStream、FileInputStream、PipedInputStream、StringBufferInputStream等类扮演。它们实现了抽象构件角色所规定的接口。
● 抽象装饰(Decorator)角色:由FilterInputStream扮演。它实现了InputStream所规定的接口。
● 具体装饰(ConcreteDecorator)角色:由几个类扮演,分别是BufferedInputStream、DataInputStream以及两个不经常使用到的类LineNumberInputStream、PushbackInputStream。