码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-12 16:28:42      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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();  
    }  
}

 

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

标签:

原文地址:http://www.cnblogs.com/aniy/p/4724677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!