理解-装饰器模式-设计模式

package com.hk.ztry;

 

interface Sourcable 
{  
    public void operation();  
  
}  

class Source implements Sourcable 
{    
    public void operation() 
    {  
        System.out.println("Soure类实现了原始接口Sourcable中的方法");  
    }  
}

class Decorator1 implements Sourcable 
{        
    private Sourcable sourcable;  
    public Decorator1(Sourcable sourcable)
    {  
        super();  
        this.sourcable=sourcable;  
    }  
      
    public void operation() 
    {  
        System.out.println("before第1个装饰器");  
        sourcable.operation();  
        System.out.println("after第1个装饰器");  
    }  
}  

class Decorator2 implements Sourcable 
{  
      
    private Sourcable sourcable;  
    public Decorator2(Sourcable sourcable)
    {  
        super();  
        this.sourcable=sourcable;  
    }  
    public void operation() 
    {  
         System.out.println("before第2个装饰器");  
         sourcable.operation();  
         System.out.println("after第2个装饰器");
    }  
}  

public class TestDecorator 
{
    public static void main(String[] args) 
    {  
        Sourcable source = new Source();  
     
        //好神奇啊, 接口=new 实现类();
        //然后接口,体现出实现类的功能,
        //接口=new 装饰类(),
        //然后接口,在原有的实现类中进行了装饰功能
        Sourcable obj = new Decorator1(new Decorator2(source));  
        
        obj.operation();  
    }  
}

 

posted @ 2015-08-12 16:04  爱吃萝卜干  阅读(121)  评论(1编辑  收藏  举报