码迷,mamicode.com
首页 > 编程语言 > 详细

代理模式--java例子

时间:2017-03-02 10:43:42      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:doc   允许   tle   oid   小明   ann   ati   ide   comm   

public interface Person{

   public void eat(); }



public class Boy implements Person{
    private String name;        // 姓名

    public Boy(String name) {
        this.name = name;
    }

    @Override
    public void eat() {
        System.out.println(name+"吃饭不能吃菜");
    }

}

public class Man implements Person{
    private Person person;   // 被代理对象

    public Man (Person person) {
        this.person= person;
    }

    @Override
    public void eat() {
      
        person.eat();

        System.out.println("允许吃菜了");
     
    }

}

public class ProxyTest{

    public static void main(String[] args) {
        Person person  = new Man (new Boy("小明"));
        person.eat();
    }
}

代理模式--java例子

标签:doc   允许   tle   oid   小明   ann   ati   ide   comm   

原文地址:http://www.cnblogs.com/hututu/p/6489157.html

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