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

装饰者设计模式

时间:2016-08-12 00:57:56      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

对一个类或者接口进行装饰

以下为例子:

package cn.test.demo;
//装饰者设计模式练习
interface Work{
    void work();
}
class Son implements Work{
    private String name;
    Son(String name){
        this.name=name;
    }
    @Override
    public void work() {
        // TODO Auto-generated method stub
        System.out.println("画画");
    }
    
}
class Monther implements Work{
    Work work;
    private String name;
    Monther(String name,Work work){
        
        this.name=name;
        this.work=work;
    }
    @Override
    public void work() {
        // TODO Auto-generated method stub
        work.work();
        System.out.println("上涂料");
    }
    
}
class Father implements Work{
    String name;
    Work work;
    Father(String name,Work work){
        this.name=name;
        this.work=work;
    }
    @Override
    public void work() {
        // TODO Auto-generated method stub
        work.work();
        System.out.println("上画框");
    }
    
}
public class Test9 {
    public static void main(String[] args) {
        Son s=new Son("小明");
        s.work();
        Monther m=new Monther("母亲", s);
        m.work();
        Father father =new Father("父亲", m);
        father.work();
    }
}

 

装饰者设计模式

标签:

原文地址:http://www.cnblogs.com/may12138/p/5763248.html

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