强制代理
简单的代理模式就不再阐述了,关于强制代理这里提一下。
强制代理就是被代理对象指定一个代理者,而不是由用户指定。看下面的一个例子:
public interface IStudent {
void doHomework();
}
public class XiaoMingProxy implements IStudent {
@Override
public void doHomework() {
System.out.println("do homework");
}
}
public class XiaoMing implements IStudent {
private XiaoMingProxy proxy;
@Override
public void doHomework() {
if (proxy == null) {
throw new RuntimeException("Please assign a proxy for XiaoMing by calling method assignProxy().");
} else {
proxy.doHomework();
}
}
public void assignProxy() {
if (proxy == null) {
proxy = new XiaoMingProxy();
}
}
}
public class Main {
public static void main(String[] args) {
XiaoMing xm = new XiaoMing();
xm.assignProxy();
xm.doHomework();
}
}
很好理解,被代理对象 XiaoMing 自己指定了一个代理对象,如果未指定则会抛出异常,且指定代理对象必须使用其提供的方法。
动态代理
普通的代理和强制代理都属于静态代理,而代理模式的重点在于动态代理。
动态代理不依赖于某个具体的代理对象,而是动态地分配代理对象。
动态代理涉及到 JDK 中两个重要的类:Proxy 和 InvocationHandler。
Proxy 负责生成具有相同接口的代理对象,而 InvocationHandler 负责控制代理对象的代理行为。
public class StudentHandler implements InvocationHandler {
private IStudent student;
public StudentHandler(IStudent student) {
this.student = student;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 具体代理行为
return null;
}
public static StudentHandler newInstance(IStudent student) {
return new StudentHandler(student);
}
}
public class DynamicProxy {
public static T newProxyInstance(ClassLoader loader,
Class>[] interfaces,
InvocationHandler h) {
return (T) Proxy.newProxyInstance(loader, interfaces, h);
}
}
public class StudentProxy extends DynamicProxy {
public static T newProxyInstance(IStudent student) {
if (student == null) {
return null;
}
return newProxyInstance(student.getClass().getClassLoader(),
student.getClass().getInterfaces(), StudentHandler.newInstance(student));
}
}
public class Main {
public static void main(String[] args) {
XiaoMing xm = new XiaoMing();
IStudent xmProxy = StudentProxy.newProxyInstance(xm);
xmProxy.doHomework();
}
}
代理模式与装饰模式的区别
代理模式和装饰模式非常相似,可以理解为装饰模式是代理模式的一种特殊代理,但是还是可以看到两者的细微差别。
代理模式中的被代理对象不对代理者进行功能的改变,而是限定其功能的执行,或者在某种条件下调用被代理者的方法,着重对被代理者的行为进行控制。
装饰模式重在对代理者功能的增强或减弱,不对准入条件进行判断,不对准入参数进行过滤。
本文由Fynn_原创,未经许可,不得转载!