java 装饰器模式
2011-12-27 09:46
253 查看
记得以前听一个高手说java.io包采用了了装饰器模式,当时不是很明白什么事装饰器模式,现在,网上看了一下说明和帮助的例子,他们的例子是这样的。
定义
Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样。因此,装饰器模式具有如下的特征:
它必须具有一个装饰的对象。
它必须拥有与被装饰对象相同的接口。
它可以给被装饰对象添加额外的功能。
用一句话总结就是:保持接口,增强性能。
装饰器通过包装一个装饰对象来扩展其功能,而又不改变其接口,这实际上是基于对象的适配器模式的一种变种。它与对象的适配器模式的异同点如下。
相同点:都拥有一个目标对象。
不同点:适配器模式需要实现另外一个接口,而装饰器模式必须实现该对象的接口。
Sourcable类的源代码如程序 12-22 所示,其定义了一个接口函数 operation() 。
程序12-22 源接口 Sourcable.java
package pattern.decorator;
public interface Sourcable {
public void operation();
}
(2 ) Source.java 是 Sourcable.java 的一个实现,其函数 operation() 负责往控制台输出一个字符串:原始类的方法。其源代码如程序 12-23 所示。
程序12-23 源类 Source.java
1.package pattern.decorator;
2.
3.public class Source implements Sourcable {
4.
5. public void operation() {
6. System.out.println("原始类的方法");
7. }
8.
9.}
(3 )装饰器类 Decorator1.java 采用了典型的对象适配器模式,它首先拥有一个 Sourcable 对象 source ,该对象通过构造函 数进行初始化。然后它实现了 Sourcable.java 接口,以期保持与 source 同样的接口,并在重写的 operation() 函数中调用 source 的 operation() 函数,在调用前后可以实现自己的输出,这就是装饰器所扩展的功能。其源代码如程序 12-24 所示。
程序12-24 装饰器类 Decorator1.java
1.package pattern.decorator;
2.
3.public class Decorator1 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator1(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10.
11. public void operation() {
12. System.out.println("第一个装饰器前");
13. sourcable.operation();
14. System.out.println("第一个装饰器后");
15.
16. }
17.
18.}
装饰器类Decorator2.java 是另一个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-25 所示。
程序12-25 装饰器类 Decorator2.java
1.package pattern.decorator;
2.
3.public class Decorator2 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator2(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10. public void operation() {
11. System.out.println("第二个装饰器前");
12. sourcable.operation();
13. System.out.println("第二个装饰器后");
14.
15. }
16.
17.}
装饰器类Decorator1.java 是第三个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-26 所示。
程序12-26 装饰器类 Decorator3.java
1.package pattern.decorator;
2.
3.public class Decorator3 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator3(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10. public void operation() {
11. System.out.println("第三个装饰器前");
12. sourcable.operation();
13. System.out.println("第三个装饰器后");
14.
15. }
16.
17.}
这时,我们就可以像使用对象的适配器模式一样来使用这些装饰器,使用不同的装饰器就可以达到不同的装饰效果。如程序12-27 所示,首先需要创建一 个源类对象 source ,然后根据将对象使用 Decorator1 来装饰,并以此使用 Decorator2 、 Decorator3 进行装饰,装饰后的对象 同样具有与 source 同样的接口。
程序12-27 测试类 DecoratorTest.java
1.package pattern.decorator;
2.
3.public class DecoratorTest {
4.
5. /**
6. * @param args
7. */
8. public static void main(String[] args) {
9. Sourcable source = new Source();
10.
11. // 装饰类对象
12. Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source)));
13. obj.operation();
14. }
15.
16.}
运行该程序的输出如下:
第1 个装饰器装饰前
第2 个装饰器装饰前
第3 个装饰器装饰前
原始类的方法
第3 个装饰器装饰后
第2 个装饰器装饰后
第1 个装饰器装饰后
从输出的结果可以看出,原始类对象source 依次被 Decorator1 、 Decorator2 、 Decorator3 进行了装饰。
定义
Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样。因此,装饰器模式具有如下的特征:
它必须具有一个装饰的对象。
它必须拥有与被装饰对象相同的接口。
它可以给被装饰对象添加额外的功能。
用一句话总结就是:保持接口,增强性能。
装饰器通过包装一个装饰对象来扩展其功能,而又不改变其接口,这实际上是基于对象的适配器模式的一种变种。它与对象的适配器模式的异同点如下。
相同点:都拥有一个目标对象。
不同点:适配器模式需要实现另外一个接口,而装饰器模式必须实现该对象的接口。
Sourcable类的源代码如程序 12-22 所示,其定义了一个接口函数 operation() 。
程序12-22 源接口 Sourcable.java
package pattern.decorator;
public interface Sourcable {
public void operation();
}
(2 ) Source.java 是 Sourcable.java 的一个实现,其函数 operation() 负责往控制台输出一个字符串:原始类的方法。其源代码如程序 12-23 所示。
程序12-23 源类 Source.java
1.package pattern.decorator;
2.
3.public class Source implements Sourcable {
4.
5. public void operation() {
6. System.out.println("原始类的方法");
7. }
8.
9.}
(3 )装饰器类 Decorator1.java 采用了典型的对象适配器模式,它首先拥有一个 Sourcable 对象 source ,该对象通过构造函 数进行初始化。然后它实现了 Sourcable.java 接口,以期保持与 source 同样的接口,并在重写的 operation() 函数中调用 source 的 operation() 函数,在调用前后可以实现自己的输出,这就是装饰器所扩展的功能。其源代码如程序 12-24 所示。
程序12-24 装饰器类 Decorator1.java
1.package pattern.decorator;
2.
3.public class Decorator1 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator1(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10.
11. public void operation() {
12. System.out.println("第一个装饰器前");
13. sourcable.operation();
14. System.out.println("第一个装饰器后");
15.
16. }
17.
18.}
装饰器类Decorator2.java 是另一个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-25 所示。
程序12-25 装饰器类 Decorator2.java
1.package pattern.decorator;
2.
3.public class Decorator2 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator2(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10. public void operation() {
11. System.out.println("第二个装饰器前");
12. sourcable.operation();
13. System.out.println("第二个装饰器后");
14.
15. }
16.
17.}
装饰器类Decorator1.java 是第三个装饰器,不同的是它装饰的内容不一样,即输出了不同的字符串。其源代码如程序 12-26 所示。
程序12-26 装饰器类 Decorator3.java
1.package pattern.decorator;
2.
3.public class Decorator3 implements Sourcable {
4.
5. private Sourcable sourcable;
6. public Decorator3(Sourcable sourcable){
7. super();
8. this.sourcable=sourcable;
9. }
10. public void operation() {
11. System.out.println("第三个装饰器前");
12. sourcable.operation();
13. System.out.println("第三个装饰器后");
14.
15. }
16.
17.}
这时,我们就可以像使用对象的适配器模式一样来使用这些装饰器,使用不同的装饰器就可以达到不同的装饰效果。如程序12-27 所示,首先需要创建一 个源类对象 source ,然后根据将对象使用 Decorator1 来装饰,并以此使用 Decorator2 、 Decorator3 进行装饰,装饰后的对象 同样具有与 source 同样的接口。
程序12-27 测试类 DecoratorTest.java
1.package pattern.decorator;
2.
3.public class DecoratorTest {
4.
5. /**
6. * @param args
7. */
8. public static void main(String[] args) {
9. Sourcable source = new Source();
10.
11. // 装饰类对象
12. Sourcable obj = new Decorator1(new Decorator2(new Decorator3(source)));
13. obj.operation();
14. }
15.
16.}
运行该程序的输出如下:
第1 个装饰器装饰前
第2 个装饰器装饰前
第3 个装饰器装饰前
原始类的方法
第3 个装饰器装饰后
第2 个装饰器装饰后
第1 个装饰器装饰后
从输出的结果可以看出,原始类对象source 依次被 Decorator1 、 Decorator2 、 Decorator3 进行了装饰。
相关文章推荐
- Java 装饰设计模式
- Java设计模式之装饰模式
- 【ThinkingInJava】43、与动态代理混合的装饰器模式
- java设计模式之结构型模式-装饰模式
- Java设计模式之装饰模式
- java设计模式之装饰模式(在i/o流中的应用)
- java设计模式--装饰模式
- 23种设计模式java实例代码之装饰模式
- Java设计模式之结构型模式-装饰器模式(Decorator)
- java设计模式学习笔记之装饰模式
- java中常用的设计模式-装饰设计模式
- Java基础---Java---IO流-----BufferedReader、BufferedWriter、缓冲区、装饰设计模式及和继承的区别
- Java设计模式----------装饰模式(Decorator)
- 黑马程序员---Java 装饰设计模式
- java与设计模式之装饰装者模式
- java设计模式--装饰模式
- java程序员从笨鸟到菜鸟之(三十八)IO流之装饰器模式初探
- Java-----装饰模式
- Java设计模式之六:装饰模式
- Java中IO流缓冲区的装饰模式的体现 推荐