代理模式-保护代理

案例场景,相亲网,

  • 本人不能对自己打分,只能修改自己的基本信息
  • 其他人只能对别人打分,不能修改别人的基本信息

代码实现

接口

package headfirst.hd.proxy.eg2;

public interface PersonBean {
    String getName();
    String getGender();
    String getInterests();
    int getHotOrNotRating();

    void setName(String name);
    void setGender(String gender);
    void setInterests(String interests);
    void setHotOrNotRating(int hotOrNotRating);
}

实现

package headfirst.hd.proxy.eg2;


public class PersonBeanImpl implements PersonBean {

    String name;
    String gender;
    String interests;
    int rating;
    int ratingCount = 0;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getGender() {
        return gender;
    }

    @Override
    public String getInterests() {
        return interests;
    }

    @Override
    public int getHotOrNotRating() {
        if (ratingCount == 0) {
            return 0;
        }

        return rating / ratingCount;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public void setInterests(String interests) {
        this.interests = interests;
    }

    @Override
    public void setHotOrNotRating(int hotOrNotRating) {
        this.ratingCount = this.ratingCount + 1;
        this.rating = this.rating + hotOrNotRating;
    }

}

非本人代理

package headfirst.hd.proxy.eg2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class NonOwnerIncocationHandler implements InvocationHandler {

    PersonBean PersonBean;

    public NonOwnerIncocationHandler(headfirst.hd.proxy.eg2.PersonBean personBean) {
        PersonBean = personBean;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("setHotOrNotRating")) {
            return method.invoke(PersonBean, args);
        } else if (method.getName().startsWith("set")) {
            System.out.println("只能对他人打分");
        } else if (method.getName().startsWith("get")) {
            return method.invoke(PersonBean, args);
        } 

        return null;
    }

}

本人代理

package headfirst.hd.proxy.eg2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class OwnerIncocationHandler implements InvocationHandler {

    PersonBean PersonBean;

    public OwnerIncocationHandler(headfirst.hd.proxy.eg2.PersonBean personBean) {
        PersonBean = personBean;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if (method.getName().startsWith("get")) {
            return method.invoke(PersonBean, args);
        } else if (method.getName().equals("setHotOrNotRating")) {
            System.out.println("无法对本人打分");
        } else if (method.getName().startsWith("set")) {
            return method.invoke(PersonBean, args);
        }

        return null;
    }

}

简单工厂方法

package headfirst.hd.proxy.eg2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class PersonSimpleFactory {

    static Map<String, PersonBean> persons = new HashMap<String, PersonBean>();

    static {
        PersonBean personBean = new PersonBeanImpl();
        personBean.setName("张三");
        personBean.setGender("男");
        persons.put("张三", personBean);
        personBean = new PersonBeanImpl();
        personBean.setName("李四");
        personBean.setGender("男");
        persons.put("李四", personBean);
        personBean = new PersonBeanImpl();
        personBean.setName("王五");
        personBean.setGender("女");
        persons.put("王五", personBean);
    }

    /**
     * @param name, 需要获取的个人姓名
     * @param opName, 操作员姓名
     */
    public static PersonBean getPerson(String name, String opName) {
        PersonBean personBean = persons.get(name);
        if (personBean == null) {
            return null;
        }

        InvocationHandler handler = null;
        if (name.equals(opName)) {
            handler = new OwnerIncocationHandler(personBean);
        } else {
            handler = new NonOwnerIncocationHandler(personBean);
        }

        return (PersonBean) Proxy.newProxyInstance(personBean.getClass().getClassLoader(), personBean.getClass().getInterfaces(), handler);
    }

}

测试代码

package headfirst.hd.proxy.eg2;

public class PersonDriveTest {

    public static void main(String[] args) {
        PersonBean ownerPerson = PersonSimpleFactory.getPerson("张三", "张三");
        PersonBean nonPerson = PersonSimpleFactory.getPerson("张三", "李四");
        PersonBean nonPerson2 = PersonSimpleFactory.getPerson("张三", "王五");

        //本人对自己打分,100分
        ownerPerson.setHotOrNotRating(100);
        ownerPerson.setInterests("唱歌");

        nonPerson.setHotOrNotRating(7);
        nonPerson2.setHotOrNotRating(9);

        //李四修改张三数据
        nonPerson.setGender("人妖");
        System.out.println(ownerPerson.getGender());
        System.out.println(ownerPerson.getHotOrNotRating());
    }

}

测试结果

无法对本人打分
只能对他人打分
男
8

猜你喜欢

转载自blog.csdn.net/dengjili/article/details/79767578
今日推荐