java设计模式-装饰模式

上传人:xzh****18 文档编号:41826306 上传时间:2018-05-31 格式:DOC 页数:4 大小:32.50KB
返回 下载 相关 举报
java设计模式-装饰模式_第1页
第1页 / 共4页
java设计模式-装饰模式_第2页
第2页 / 共4页
java设计模式-装饰模式_第3页
第3页 / 共4页
java设计模式-装饰模式_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《java设计模式-装饰模式》由会员分享,可在线阅读,更多相关《java设计模式-装饰模式(4页珍藏版)》请在金锄头文库上搜索。

1、java 设计模式-装饰模式Decorator 装饰器,顾名思义,就是动态地给一个对象添加一些额外的职 责,就好比为房子进行装修一样。因此,装饰器模式具有如下的特征:它必须具有一个装饰的对象。它必须拥有与被装饰对象相同的接口。它可以给被装饰对象添加额外的功能。用一句话总结就是:保持接口,增强性能。装饰器通过包装一个装饰对象来扩展其功能,而又不改变其接口,这实际 上是基于对象的适配器模式的一种变种。它与对象的适配器模式的异同点如下。相同点:都拥有一个目标对象。不同点:适配器模式需要实现另外一个接口,而装饰器模式必须实现该对 象的接口。Sourcable 类的源代码如程序 12-22 所示,其定义

2、了一个接口函数 operation() 。程序 12-22 源接口 Sourcable.java(2 ) Source.jpackage decorator;public interface Sourcable public void operation();Source 是 Sourcable.java 的一个实现,其函数 operation() 负责往控制 台输出一个字符串:原始类的方法。其源代码如程序 12-23 所示。程序 12-23 源类 Source.javapackage decorator;public class Source implements Sourcablepubl

3、ic void operation() / TODO Auto-generated method stubSystem.out.println(“原始类的方法“);(3 )装饰器类 Decorator1.java 采用了典型的对象适配器模式,它首先 拥有一个 Sourcable 对象 source ,该对象通过构造函 数进行初始化。然后 它实现了 Sourcable.java 接口,以期保持与 source 同样的接口,并在重写 的 operation() 函数中调用 source 的 operation() 函数,在调用前后可以实 现自己的输出,这就是装饰器所扩展的功能。其源代码如程序 12

4、-24 所示。程序 12-24 装饰器类 Decorator1.javapackage decorator;public class Decorator1 implements Sourcableprivate Sourcable sourcable; public Decorator1(Sourcable sourcable) super(); this.sourcable=sourcable; public void operation() System.out.println(“第一个装饰器前“); sourcable.operation(); System.out.println(“第

5、一个装饰器后“); 装饰器类 Decorator2.java 是另一个装饰器,不同的是它装饰的内容不一 样,即输出了不同的字符串。其源代码如程序 12-25 所示。程序 12-25 装饰器类 Decorator2.javapackage decorator;public class Decorator2 implements Sourcableprivate Sourcable sourcable; public Decorator2(Sourcable sourcable) super(); this.sourcable=sourcable; public void operation()

6、System.out.println(“第二个装饰器前“); sourcable.operation(); System.out.println(“第二个装饰器后“); 装饰器类 Decorator1.java 是第三个装饰器,不同的是它装饰的内容不一 样,即输出了不同的字符串。其源代码如程序 12-26 所示。程序 12-26 装饰器类 Decorator3.javapackage decorator;public class Decorator3 implements Sourcableprivate Sourcable sourcable; public Decorator3(Sourc

7、able sourcable) super(); this.sourcable=sourcable; public void operation() System.out.println(“第三个装饰器前“); sourcable.operation(); System.out.println(“第三个装饰器后“); 这时,我们就可以像使用对象的适配器模式一样来使用这些装饰器,使用 不同的装饰器就可以达到不同的装饰效果。如程序 12-27 所示,首先需要创建 一 个源类对象 source ,然后根据将对象使用 Decorator1 来装饰,并以此使 用 Decorator2 、 Decorat

8、or3 进行装饰,装饰后的对象 同样具有与 source 同样的接口。程序 12-27 测试类 DecoratorTest.javapackage decorator;public class DecoratorTest public static void main(String args) / TODO Auto-generated method stubSourcable source = new Source(); / 装饰类对象 Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source); obj.operation(); 运行该程序的输出如下:第 1 个装饰器装饰前第 2 个装饰器装饰前第 3 个装饰器装饰前原始类的方法第 3 个装饰器装饰后第 2 个装饰器装饰后第 1 个装饰器装饰后从输出的结果可以看出,原始类对象 source 依次被 Decorator1 、 Decorator2 、 Decorator3 进行了装饰。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号