装饰者模式

来源:互联网 发布:淘宝直通车质量得分 编辑:程序博客网 时间:2024/05/22 01:50

装饰者设计模式:增强一个类的功能,而且还可以让这些装饰类互相装饰

1.在装饰类的内部维护一个被装饰类的引用。2.让装饰类有一个共同的父类或者是父接口。
interface work{    public void work();}class son implements work{    public void work(){        System.out.println("画画");        }}class mother implements work{    work  w;    public mother(work w){        this.w=w;    }    public void work(){        w.work();        System.out.println("油彩");    }}class father implements work{    work w;    public father(work w){        this.w=w;    }    public void work(){        w.work();        System.out.println("装裱");    }}public class compare{    public static void main(String[] args){        son s=new son();        s.work();        System.out.println("********");        mother m=new mother(s);        m.work();        System.out.println("********");        father f=new father(m);        f.work();    }}
0 0