java装饰器_java里的装饰器.请具体回答其作用和用法?

本文介绍了Java中的装饰器模式,通过Sourcable接口和Source类展示了装饰器的基本用法。Decorator1、Decorator2和Decorator3作为装饰器类,分别在源对象的operation()方法前后添加额外的功能。在DecoratorTest测试类中,展示了如何通过组合多个装饰器来实现不同的装饰效果,输出显示了装饰器的层层叠加应用。
摘要由CSDN通过智能技术生成

展开全部

Sourcable类的源代码如程序 12-22 所示,其定62616964757a686964616fe4b893e5b19e31333332626132义了一个接口函数 operation() 。 程序12-22 源接口 Sourcable.java Java代码 package pattern.decorator; public interface Sourcable { public void operation(); } package pattern.decorator; public interface Sourcable { public void operation(); } (2 ) Source.java 是 Sourcable.java 的一个实现,其函数 operation() 负责往控制台输出一个字符串:原始类的方法。其源代码如程序 12-23 所示。 程序12-23 源类 Source.java Java代码 package pattern.decorator; public class Source implements Sourcable { public void operation() { System.out.println("原始类的方法"); } } package pattern.decorator; public class Source implements Sourcable { public void operation() { System.out.println("原始类的方法"); } } (3 )装饰器类 Decorator1.java 采用了典型的对象适配器模式,它首先拥有一个 Sourcable 对象 source ,该对象通过构造函 数进行初始化。然后它实现了 Sourcable.java 接口,以期保持与 source 同样的接口,并在重写的 operation() 函数中调用 source 的 operation() 函数,在调用前后可以实现自己的输出,这就是装饰器所扩展的功能。其源代码如程序 12-24 所示。 程序12-24 装饰器类 Decorator1.java Java代码 package pattern.decorator; public class Decorator1 implements Sourcable { private Sourcable sourcable; public Decorator1(Sourcable sourcable){ super(); this.sourcable=sourcable; } public void operation() { System.out.println("第一个装饰器前"); sourcable.operation(); System.out.println("第一个装饰器后"); } } package pattern.decorator; public class Decorator1 implements Sourcable { private Sourcable sourcable; public Decorator1(Sourcable sourcable){ super(); this.sourcable=sourcable; } public void operation() { System.out.println("第一个装饰器前"); sourcable.operation(); System.out.println("第一个装饰器后"); } } 装饰器类Decorator2.java 是另一个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-25 所示。 程序12-25 装饰器类 Decorator2.java Java代码 package pattern.decorator; public class Decorator2 implements Sourcable { private Sourcable sourcable; public Decorator2(Sourcable sourcable){ super(); this.sourcable=sourcable; } public void operation() { System.out.println("第二个装饰器前"); sourcable.operation(); System.out.println("第二个装饰器后"); } } package pattern.decorator; public class Decorator2 implements Sourcable { private Sourcable sourcable; public Decorator2(Sourcable sourcable){ super(); this.sourcable=sourcable; } public void operation() { System.out.println("第二个装饰器前"); sourcable.operation(); System.out.println("第二个装饰器后"); } } 装饰器类Decorator1.java 是第三个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-26 所示。 程序12-26 装饰器类 Decorator3.java Java代码 package pattern.decorator; public class Decorator3 implements Sourcable { private Sourcable sourcable; public Decorator3(Sourcable sourcable){ super(); this.sourcable=sourcable; } public void operation() { System.out.println("第三个装饰器前"); sourcable.operation(); System.out.println("第三个装饰器后"); } } package pattern.decorator; public class Decorator3 implements Sourcable { private Sourcable sourcable; public Decorator3(Sourcable sourcable){ super(); this.sourcable=sourcable; } public void operation() { System.out.println("第三个装饰器前"); sourcable.operation(); System.out.println("第三个装饰器后"); } } 这时,我们就可以像使用对象的适配器模式一样来使用这些装饰器,使用不同的装饰器就可以达到不同的装饰效果。如程序12-27 所示,首先需要创建一 个源类对象 source ,然后根据将对象使用 Decorator1 来装饰,并以此使用 Decorator2 、 Decorator3 进行装饰,装饰后的对象 同样具有与 source 同样的接口。 程序12-27 测试类 DecoratorTest.java Java代码 package pattern.decorator; public class DecoratorTest { /** * @param args */ public static void main(String[] args) { Sourcable source = new Source(); // 装饰类对象 Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source))); obj.operation(); } } package pattern.decorator; public class DecoratorTest { /** * @param args */ public static void main(String[] args) { Sourcable source = new Source(); // 装饰类对象 Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source))); obj.operation(); } } 运行该程序的输出如下: 第1 个装饰器装饰前 第2 个装饰器装饰前 第3 个装饰器装饰前 原始类的方法 第3 个装饰器装饰后 第2 个装饰器装饰后 第1 个装饰器装饰后 从输出的结果可以看出,原始类对象source 依次被 Decorator1 、 Decorator2 、 Decorator3 进行了装饰。

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值