代理模式:为另一个对象提供一个替身或占位符以访问这个对象。代理模式为另一个对象提供代表,以便控制客户对对象的访问,管理访问的方式有许多种。
远程代理管理客户和远程对象之间的交互。
虚拟代理控制访问实例化开销大的对象。
保护代理基于调用者控制对对象方法的访问。
代理模式有许多变体,例如:缓存代理、同步代理、防火墙代理、写入时复制代理。
Java内置的代理支持,可以根据需要动态创建代理,并将所有调用分配到所选的调用处理器(InvocationHandler)。
下面 的例子是一种保护代理
类图:
源代码如下:
package javaproxy; /** * 被代理者接口 * @author Arvon * */ public interface PersonBean { public String getName(); public String getGender(); public String getInterests(); public int getHotAndNotRating(); public void setHotAndNotRating(int rating); public void setInterests(String interests); public void setName(String name); public void setGender(String gender); }
package javaproxy; public class PersonBeanImpl implements PersonBean { String name; String gender; String interests; int rating, ratingCount =1; public PersonBeanImpl(String name, String gender, String interests, int rating) { super(); this.name = name; this.gender = gender; this.interests = interests; this.rating = rating; } @Override public String toString() { return "PersonBeanImpl [name=" + name + ", gender=" + gender + ", interests=" + interests + ", rating=" + getHotAndNotRating() + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getInterests() { return interests; } public void setInterests(String interests) { this.interests = interests; } @Override public int getHotAndNotRating() { return ratingCount==0?0:(rating/ratingCount); } public void setHotAndNotRating(int rating){ this.rating+=rating; ratingCount++; } }
package javaproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; /** * InvocationHandler 负责处理代理类发来的请求(即代理类的调用) * @author Administrator * */ public class OwnerInvocationHandler implements InvocationHandler { PersonBean person; public OwnerInvocationHandler(PersonBean person) { super(); this.person = person; } /** * Parameters:proxy - the proxy instance that the method was invoked * onmethod - the Method instance corresponding to the interface method * invoked on the proxy instance. The declaring class of the Method object * will be the interface that the method was declared in, which may be a * superinterface of the proxy interface that the proxy class inherits the * method through.args - an array of objects containing the values of the * arguments passed in the method invocation on the proxy instance, or null * if interface method takes no arguments. Arguments of primitive types are * wrapped in instances of the appropriate primitive wrapper class, such as * java.lang.Integer or java.lang.Boolean.Returns:the value to return from * the method invocation on the proxy instance. If the declared return type * of the interface method is a primitive type, then the value returned by * this method must be an instance of the corresponding primitive wrapper * class; otherwise, it must be a type assignable to the declared return * type. If the value returned by this method is null and the interface * method's return type is primitive, then a NullPointerException will be * thrown by the method invocation on the proxy instance. If the value * returned by this method is otherwise not compatible with the interface * method's declared return type as described above, a ClassCastException * will be thrown by the method invocation on the proxy instance. */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws IllegalAccessException { try { if(method.getName().startsWith("get")) return method.invoke(person, args); //方法的执行者 person else if(method.getName().equals("setHotAndNotRating")) throw new IllegalAccessException(); else if(method.getName().startsWith("set")) return method.invoke(person, args); //方法的执行者 person } catch (Exception e) { // e.printStackTrace(); System.out.println("cannot set rating by owner proxy!"); } return null;//如果调用其它的方法一律不予处理 } }
package javaproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class NonOwnerInvocationHandler implements InvocationHandler{ PersonBean person; public NonOwnerInvocationHandler(PersonBean person) { super(); this.person = person; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if(method.getName().startsWith("get")) return method.invoke(person, args); //方法的执行者 person else if(method.getName().equals("setHotAndNotRating")) return method.invoke(person, args); else if(method.getName().startsWith("set"))//他人不能设置自己的信息 throw new IllegalAccessException(); } catch (Exception e) { System.out.println("cannot set infos from nonowner proxy..."); } return null;//如果调用其它的方法一律不予处理 } }
package javaproxy; import java.lang.reflect.Proxy; /** * 动态产生代理类 * @author Administrator * */ public class ProxyHelper { /** * Returns an instance of a proxy class for the specified interfaces that * dispatches method invocations to the specified invocation handler. * @param person * @return */ public static PersonBean getOwnerProxy(PersonBean person) { return (PersonBean) Proxy.newProxyInstance(person.getClass().getClassLoader(), person.getClass().getInterfaces(), new OwnerInvocationHandler(person)); } public static PersonBean getNonOwnerProxy(PersonBean person) { return (PersonBean) Proxy.newProxyInstance(person.getClass().getClassLoader(), person.getClass().getInterfaces(), new NonOwnerInvocationHandler(person)); } }
package javaproxy; /** * 测试类 * @author Administrator * */ public class ProxyTest { public static void main(String[] args) { PersonBean person = new PersonBeanImpl("Jay", "F", "Java", 7); System.out.println(person); //动态地产生代理类ownerProxy (代理类ownerProxy实现了PersonBean接口) PersonBean ownerProxy = ProxyHelper.getOwnerProxy(person); System.out.println("Name is "+ownerProxy.getName()); //代理类的方法setInterests被调用 代理ownerProxy会把这个调用转发给OwnerInvocationHandler //ownerProxy 被实例化的时候指定的调用处理器是OwnerInvocationHandler // 接着 OwnerInvocationHandler 调用它自己的invoke方法 //OwnerInvocationHandler 可能会转发给被代理者PersonBean person 处理,如 return method.invoke(person, args); //方法的执行者 person // 也可能做其他的处理 如抛出异常 ownerProxy.setInterests("python"); System.out.println(person); try { ownerProxy.setHotAndNotRating(8); } catch (Exception e) { } PersonBean nonOwnerProxy = ProxyHelper.getNonOwnerProxy(person); System.out.println("Name is "+nonOwnerProxy.getName()); try { nonOwnerProxy.setHotAndNotRating(9); nonOwnerProxy.setInterests("python"); } catch (Exception e) { } System.out.println(person); } }
程序的输出:
PersonBeanImpl [name=Jay, gender=F, interests=Java, rating=7]
Name is Jay
PersonBeanImpl [name=Jay, gender=F, interests=python, rating=7]
cannot set rating by owner proxy!
Name is Jay
cannot set infos from nonowner proxy...
PersonBeanImpl [name=Jay, gender=F, interests=python, rating=8]
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-10 04:12:04