1700465044
1700465045
❑具体装饰角色
1700465046
1700465047
ConcreteDecoratorA和ConcreteDecoratorB是两个具体的装饰类,你要把你最核心的、最原始的、最基本的东西装饰成其他东西,上面的例子就是把一个比较平庸的成绩单装饰成家长认可的成绩单。
1700465048
1700465049
装饰模式的所有角色都已经解释完毕,我们来看看如何实现,先看抽象构件,如代码清单17-10所示。
1700465050
1700465051
代码清单17-10 抽象构件
1700465052
1700465053
public abstract class Component{
1700465054
1700465055
//抽象的方法
1700465056
1700465057
public abstract void operate();
1700465058
1700465059
}
1700465060
1700465061
具体构件如代码清单17-11所示。
1700465062
1700465063
代码清单17-11 具体构件
1700465064
1700465065
public class ConcreteComponent extends Component{
1700465066
1700465067
//具体实现
1700465068
1700465069
@Override
1700465070
1700465071
public void operate(){
1700465072
1700465073
System.out.println(“do Something”);
1700465074
1700465075
}
1700465076
1700465077
}
1700465078
1700465079
装饰角色通常是一个抽象类,如代码清单17-12所示。
1700465080
1700465081
代码清单17-12 抽象装饰者
1700465082
1700465083
public abstract class Decorator extends Component{
1700465084
1700465085
private Component component=null;
1700465086
1700465087
//通过构造函数传递被修饰者
1700465088
1700465089
public Decorator(Component_component){
1700465090
1700465091
this.component=_component;
1700465092
1700465093
}
[
上一页 ]
[ :1.700465044e+09 ]
[
下一页 ]