代理模式-动态代理

package M_Proxy.dynamic;

import java.lang.reflect.Proxy;

/**
 *使用动态代理完成保护代理
 *
 */
public class MatchMakingTestDrive {
	
	public static void main(String[] args) {
		MatchMakingTestDrive test = new MatchMakingTestDrive();
		test.drive();
	}

	private void drive() {
		// 创建对象
		PersonBean personBean = new PersonBeanImpl("张三", "女", "兴趣是上网", 10);
		// 创建拥有者对象
		PersonBean ownerProxy = getOwnerProxy(personBean);
		System.out.println(ownerProxy.getName());
		// 行得通
		ownerProxy.setInterests("bowling go");
		System.out.println(ownerProxy.getInterests());
		// 改变评分,行不通
		try {
			ownerProxy.setHotOrNotRating(10);
		} catch (Exception e) {
			System.out.println("修改评分失败");
		}

		System.out.println("============");
		// 创建非拥有者对象
		PersonBean nonOwnerProxy = getNonOwnerProxy(personBean);
		// 失败
		try {
			nonOwnerProxy.setInterests("哈哈");
		} catch (Exception e) {
			System.out.println("设置兴趣爱好失败");
		}
		// 成功
		nonOwnerProxy.setHotOrNotRating(8);
		System.out.println(nonOwnerProxy.getHotOrNotRating());

	}

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

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


package M_Proxy.dynamic;

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

/**
 * 非拥有者对象
 */
public class NonOwnerInvocationHandler implements InvocationHandler {

	PersonBean person;

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

	public Object invoke(Object proxy, Method method, Object[] args)
			throws IllegalAccessException {
		try {
			if (method.getName().startsWith("get")) {
				return method.invoke(person, args);
			} else if (method.getName().startsWith("setHotOrNotRating")) {
				return method.invoke(person, args);
			} else if (method.getName().startsWith("set")) {
				throw new IllegalAccessException();
			}
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

}
package M_Proxy.dynamic;

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

/**
 * 真正拥有者对象
 */
public class OwnerInvocationHandler implements InvocationHandler {

	PersonBean person;

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

	public Object invoke(Object proxy, Method method, Object[] args)
			throws IllegalAccessException {
		try {
			if (method.getName().startsWith("get")) {
				return method.invoke(person, args);
			} else if (method.getName().startsWith("setHotOrNotRating")) {
				throw new IllegalAccessException();
			} else if (method.getName().startsWith("set")) {
				return method.invoke(person, args);
			}
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return null;
	}

}

package M_Proxy.dynamic;

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

	public PersonBeanImpl(String name, String gender, String interests,
			int rating) {
		this.name = name;
		this.gender = gender;
		this.interests = interests;
		this.rating = rating;
		ratingCount++;
	}

	public String getName() {
		return name;
	}

	public String getGender() {

		return gender;
	}

	public String getInterests() {

		return interests;
	}

	public int getHotOrNotRating() {
		return ratingCount == 0 ? 0 : rating / ratingCount;
	}

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

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

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

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

}

package M_Proxy.dynamic;

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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值