设计模式——装饰者模式

package com.hujl.demo;
/**
 * 定义一个接口
 * @author Janrone
 * @website http://hujl.sinaapp.com
 */
public interface Worker {
    
    public void doSometing();
    
}

 

 

 

View Code
package com.hujl.demo;

/**
 * 创建一个电工类
 * @author Janrone
 * @website http://hujl.sinaapp.com
 */
public class Wireman implements Worker{
    @Override
    public void doSometing() {
        System.out.println("我是电工,负责维修电路!");
    }
}

 


 

<span style="font-family:Arial, Verdana, sans-serif;"><span style="white-space: normal;">
</span></span>

 

package com.hujl.demo;

/**
 * 创建一个清洁工类
 * @author Janrone
 * @website http://hujl.sinaapp.com
 */
public class Dustman implements Worker{
    @Override
    public void doSometing() {
        System.out.println("我是清洁工,负责打扫卫生!");
    }
}

 

package com.hujl.demo;

/**
 * 创建一个A物业公司
 * @author Janrone
 * @website http://hujl.sinaapp.com
 */
public class Aproperty implements Worker {
    
    private Worker worker;
    
    public Aproperty(Worker worker){
        this.worker=worker;
    }
    @Override
    public void doSometing() {
        // TODO Auto-generated method stub
        System.out.println("您好,A物业公司.");
        worker.doSometing();
    }
}
package com.hujl.demo;

/**
 * 创建一个B物业公司
 * @author Janrone
 * @website http://hujl.sinaapp.com
 */
public class Bproperty implements Worker {
    
    private Worker worker;

    public Bproperty(Worker worker) {
        this.worker = worker;
    }
    
    @Override
    public void doSometing() {
        // TODO Auto-generated method stub
        System.out.println("您好,B物业公司.");
        worker.doSometing();
    }
}

 

 

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        //生成一个电工对象
        Wireman worker=new Wireman();
        //生成A物业公司对象
        Aproperty aproperty=new Aproperty(worker);
        aproperty.doSometing();
        ////生成B物业公司对象
        Bproperty bproperty=new Bproperty(worker);
        bproperty.doSometing();
        
    }
}

 




posted @ 2012-04-10 12:00  tokeep  阅读(139)  评论(0编辑  收藏  举报