代理模式解决不同请求和相应的目标对象的中介作用,实现面向接口编程,封装对象的行为实现
Java API中提供了对Proxy模式的支持,主要是通过反射(Reflect)包中的Proxy类和InvocationHandler接口实现,具体过程如下:
1) 实现InvocationHandler接口,在invoke()方法中实现代理类要完成的操作;
(2) 通过Proxy.newProxyInstance(ClassLoader loader,Class[]
interfaces,InvocationHandler h)方法生成一个代理类,从参数可以看出代理类将实现被代理对象的接口,而具体的实现过程是在上面实现的InvocationHandler.invoke()中定义的.
下面是代理模式的例子
对象的行为接口
package cn.oracle.computer;
public interface Computer {
/*
*zhangleming_2007-10-17
*/
public void buy();
}
package cn.oracle.computer;
/**
* @author zhangleming_2007-10-17
*
*/
接口实现
public class IBMComputer implements Computer {
/**
* zhangleming_2007-10-17
*/
public IBMComputer() {
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see cn.oracle.computer.Computer#buy()
*/
public void buy() {
System.out.println("you had bought a IBM <>");
}
}
代理实现
package cn.oracle.computer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author zhangleming_2007-10-17
*
*/
public class ComputerProxy implements InvocationHandler {
private Object object;
/**
* zhangleming_2007-10-17
*/
public ComputerProxy() {
}
public Object bind(Object object){
this.object=object;
return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
}
/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] arg) throws Throwable {
Object result=method.invoke(object, arg);
System.out.println("赠送鼠标一个!");
return result;
}
}
测试
ComputerProxy cp=new ComputerProxy();
Computer c=(Computer) cp.bind(new IBMComputer());
c.buy();
Java API中提供了对Proxy模式的支持,主要是通过反射(Reflect)包中的Proxy类和InvocationHandler接口实现,具体过程如下:
1) 实现InvocationHandler接口,在invoke()方法中实现代理类要完成的操作;
(2) 通过Proxy.newProxyInstance(ClassLoader loader,Class[]
interfaces,InvocationHandler h)方法生成一个代理类,从参数可以看出代理类将实现被代理对象的接口,而具体的实现过程是在上面实现的InvocationHandler.invoke()中定义的.
下面是代理模式的例子
对象的行为接口
package cn.oracle.computer;
public interface Computer {
/*
*zhangleming_2007-10-17
*/
public void buy();
}
package cn.oracle.computer;
/**
* @author zhangleming_2007-10-17
*
*/
接口实现
public class IBMComputer implements Computer {
/**
* zhangleming_2007-10-17
*/
public IBMComputer() {
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see cn.oracle.computer.Computer#buy()
*/
public void buy() {
System.out.println("you had bought a IBM <>");
}
}
代理实现
package cn.oracle.computer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author zhangleming_2007-10-17
*
*/
public class ComputerProxy implements InvocationHandler {
private Object object;
/**
* zhangleming_2007-10-17
*/
public ComputerProxy() {
}
public Object bind(Object object){
this.object=object;
return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
}
/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] arg) throws Throwable {
Object result=method.invoke(object, arg);
System.out.println("赠送鼠标一个!");
return result;
}
}
测试
ComputerProxy cp=new ComputerProxy();
Computer c=(Computer) cp.bind(new IBMComputer());
c.buy();