設計模式: 自己手動寫一個代理模式

代理模式:爲另一個對象提供一個替身或佔位符以訪問這個對象。代理模式爲另一個對象提供代表,以便控制客戶對對象的訪問,管理訪問的方式有許多種。

遠程代理管理客戶和遠程對象之間的交互。

虛擬代理控制訪問實例化開銷大的對象。

保護代理基於調用者控制對對象方法的訪問。

代理模式有許多變體,例如:緩存代理、同步代理、防火牆代理、寫入時複製代理。

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]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章