Java动态代理模式

代理模式
  看完之后我压根不知道这个是在做什么,虽然确实做了代理,但这样的必要性在哪里一直没想通,或许现在的例子比较简单无法看出来吧。

  就是一个类,创建了两个不同的代理,代理能够进行不同的操作,对于禁止的操作可以做一个异常处理,功能就是这样,限制用户。貌似用了做权限操作很不错。

代码没注释,如果看不懂只能说得好好学基础再来看。

package ProxyMode.Person;

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 rating);
}
package ProxyMode.Person;

public class PersonBeanImpl implements PersonBean {
    String name;
    String gender;
    String interests;
    int rating;
    int ratingCount = 0;

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

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

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

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

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

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

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

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

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

public class OwnerInvocationHandler implements InvocationHandler {
    PersonBean person;
    public OwnerInvocationHandler(PersonBean person) {
        this.person = person;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        if (method.getName().startsWith("get")) {
            return method.invoke(person, args);
        } else if (method.getName().equals("setHotOrNotRating")) {
            throw new IllegalAccessException();
        } else if (method.getName().startsWith("set")) {
            return method.invoke(person, args);
        }
        return null;
    }
}
package ProxyMode.Person;

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

public class NonOwnerInvocationHandler implements InvocationHandler {
    PersonBean person;

    public NonOwnerInvocationHandler(PersonBean person) {
        this.person = person;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        if (method.getName().startsWith("get")) {
            return method.invoke(person, args);
        } else if (method.getName().equals("setHotOrNotRating")) {
            return method.invoke(person, args);
        } else if (method.getName().startsWith("set")) {
            throw new IllegalAccessException();
        }
        return null;
    }
}
package ProxyMode.Person;

import java.lang.reflect.Proxy;
import java.util.Random;

public class MatchMakingTestDrive {
    public static void main(String[] args) {
        MatchMakingTestDrive test = new MatchMakingTestDrive();
        test.drive();
    }

    public MatchMakingTestDrive() {
    }

    public void drive() {
        PersonBean joe = getPersonFromDatabase("Joe Javabean");
        PersonBean ownerProx = getOwnerProxy(joe);
        System.out.println("Name is " + ownerProx.getName());
        ownerProx.setInterests("bowling, Go");
        System.out.println("Intersts set from owner proxy");
        try {
            ownerProx.setHotOrNotRating(10);
        } catch (Exception e) {
            System.out.println("Can't set rating from owner proxy");
        }
        System.out.println("Rating is " + ownerProx.getHotOrNotRating());

        System.out.println();

        PersonBean nonOwnerProxy = getNonOwnerProxy(joe);
        System.out.println("Name is " + nonOwnerProxy.getName());
        try {
            nonOwnerProxy.setInterests("bowling, Go");
        } catch (Exception e) {
            System.out.println("Can't set intersets from non owner proxy");
        }
        nonOwnerProxy.setHotOrNotRating(3);
        System.out.println("Rating set from non owner proxy");
        System.out.println("Rating is " + nonOwnerProxy.getHotOrNotRating());
    }

    PersonBean getOwnerProxy(PersonBean person) {
        return (PersonBean) Proxy.newProxyInstance(person.getClass()
                .getClassLoader(), person.getClass().getInterfaces(),
                new OwnerInvocationHandler(person));
    }

    PersonBean getNonOwnerProxy(PersonBean person) {
        return (PersonBean) Proxy.newProxyInstance(person.getClass()
                .getClassLoader(), person.getClass().getInterfaces(),
                new NonOwnerInvocationHandler(person));

    }

    PersonBean getPersonFromDatabase(String name) {
        PersonBean rt = new PersonBeanImpl();
        rt.setName(name);
        Random random = new Random();
        rt.setHotOrNotRating(random.nextInt(100));
        return rt;
    }
}

当前还没有任何评论

写下你最简单的想法