打字猴:1.700465157e+09
1700465157 }
1700465158
1700465159 //重写父类的Operation方法
1700465160
1700465161 public void operate(){
1700465162
1700465163 super.operate();
1700465164
1700465165 this.method2();
1700465166
1700465167 }
1700465168
1700465169 }
1700465170
1700465171 注意 原始方法和装饰方法的执行顺序在具体的装饰类是固定的,可以通过方法重载实现多种执行顺序。
1700465172
1700465173 我们通过Client类来模拟高层模块的耦合关系,看看装饰模式是如何运行的,如代码清单17-14所示。
1700465174
1700465175 代码清单17-14 场景类
1700465176
1700465177 public class Client{
1700465178
1700465179 public static void main(String[]args){
1700465180
1700465181 Component component=new ConcreteComponent();
1700465182
1700465183 //第一次修饰
1700465184
1700465185 component=new ConcreteDecorator1(component);
1700465186
1700465187 //第二次修饰
1700465188
1700465189 component=new ConcreteDecorator2(component);
1700465190
1700465191 //修饰后运行
1700465192
1700465193 component.operate();
1700465194
1700465195 }
1700465196
1700465197 }
1700465198
1700465199
1700465200
1700465201
1700465202 设计模式之禅 [:1700453993]
1700465203 设计模式之禅 17.3 装饰模式应用
1700465204
1700465205 17.3.1 装饰模式的优点
1700465206
[ 上一页 ]  [ :1.700465157e+09 ]  [ 下一页 ]