`
Josh_Persistence
  • 浏览: 1631994 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

Java - 深入浅出装饰器模式

阅读更多

Java程序员们应该对java.io对不会陌生,因为java.io包采用了装饰器模式。

一、定义:

 

        Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样。因此,装饰器模式具有如下的特征:

 

        它必须具有一个装饰的对象。

 

        它必须拥有与被装饰对象相同的接口。

 

        它可以给被装饰对象添加额外的功能。

 

用一句话总结就是:保持接口,增强性能。

 

装饰器通过包装一个装饰对象来扩展其功能,而又不改变其接口,这实际上是基于对象的适配器模式的一种变种。它与对象的适配器模式的异同点如下。

 

       相同点:都拥有一个目标对象。

 

       不同点:适配器模式需要实现另外一个接口,而装饰器模式必须实现该对象的接口。

 

 

二、 典型的装饰器模式的图:

 

 

三、相关实例:

 

   1、 Sourcable接口定义了一个方法 operation()

 

 

 

    package pattern.decorator;  
      
    public interface Sourcable {  
        public void operation();  
      
    }  

   

 

  2、 Source.java Sourcable.java 的一个实现, operation() 方法的实现就是简单的负责往控制台输出一个字符串:

 

    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() 函数,在调用前后可以实现自己的输出,这就是装饰器所扩展的功能。

 

    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("第1个装饰器前");  
            sourcable.operation();  
            System.out.println("第1个装饰器后");  
      
        }  
      
    }  

 

 

 

装饰器类Decorator2.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("第2个装饰器前");  
            sourcable.operation();  
            System.out.println("第2个装饰器后");  
      
        }  
      
    }  

   

 

装饰器类Decorator3.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("第3个装饰器前");  
            sourcable.operation();  
            System.out.println("第3个装饰器后");  
      
        }  
      
    }  

   

 

这时,我们就可以像使用对象的适配器模式一样来使用这些装饰器,使用不同的装饰器就可以达到不同的装饰效果。如下测试类:首先需要创建一 个源类对象 source ,然后根据将对象使用 Decorator1 来装饰,并以此使用 Decorator2 Decorator3 进行装饰,装饰后的对象同样具有与 source 同样的接口。

 

 

    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 进行了装饰。

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics