代理模式-java

package u14;


public class RealBBS implements BBS {

    @Override
    public void function() {
        // TODO Auto-generated method stub

        System.out.println("欢迎您的光临");
        System.out.println("您的条件为:漂亮");
    }

}
package u14;


public class Proxy implements BBS {
    private RealBBS bbs = new RealBBS();// 维持一个对对真实主题对象的引用
    private int permission = 2; // 权限
    private String name;

    public Proxy(String name, int permission) {
        this.name = name;
        this.permission = permission;
    }

    @Override
    public void function() {
        // TODO Auto-generated method stub
        if (permission >= 2) {
            System.out.println("-----------------");
            System.out.println("尊敬的客户您" + this.name);
            bbs.function();
        } else {
            System.out.println("-----------------");
            System.out.println("您" + this.name + "?对不起,不能早恋!");
        }
    }

}
package u14;


public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BBS s1, s2;
        s1 = new Proxy("35岁", 2); // 权限为2
        s1.function();

        s2 = new Proxy("15岁", 1); // 权限为1
        s2.function();
    }

}
package u14;

public interface BBS {
    public void function();
}

 

posted @ 2021-10-22 21:17  yasai  阅读(50)  评论(0)    收藏  举报