首页 > 代码库 > java:装饰模式,节点流和处理流

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