java:装饰者模式,节点流和处理流

接口:

interface Worker
{
    public void dowork();
}

清洁工:被装饰者,节点流

class Clean implements Worker
{
    public void dowork(){
        System.out.println("搞清洁");
    }
}

A公司:装饰者,处理流

class Aworker implements Worker
{
    private Worker worker;    //传进来的对象是woker的实现类
    public Aworker(Worker worker){
        this.worker = worker;
    }
    public void dowork(){
        System.out.println("A公司");
        worker.dowork();
    }
}

实例化:

class Test
{
    public static void main(String args[]){
        Clean clean = new Clean();
        Aworker worker1 = new Aworker(clean);
        worker1.dowork();
    }
}

 

posted @ 2014-05-26 17:31  tinyphp  Views(324)  Comments(0Edit  收藏  举报