首页 > 代码库 > 代理模式--java例子

代理模式--java例子

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例子