定义:为其他对象提供一种代理以控制对这个对象的访问。
实现组成:
-
抽象角色:通过接口或抽象类声明真实角色实现的业务方法。
-
代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。
-
真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用
两种实现方式:静态代理、动态代理
静态代理模拟的代码:
1、抽象角色(代理实现的方法)
public interface ILog {
public String printLog();
}
2、真实角色(被代理的类)
public class Dog implements ILog {
public Dog(){
}
public String printLog(){
System.out.println("this is a Dog.");
return "end";
}
}
3、代理角色(代理)
public class StaticProxy implements ILog {
ILog target;
public StaticProxy(ILog target) {
this.target = target;
}
@Override
public String printLog() {
System.out.println("before print");
String result = target.print();
System.out.println("after print");
return "print " + result;
}
}
4、测试类
public static void main(String[] args) {
IDemo demo = new StaticProxy(new Demo());
System.out.println(demo.print());
}
动态代理模拟的代码:
1、抽象角色(代理实现的内容)
public interface ILog {
public String printLog();
}
2、真实角色(被代理的类)
public class Dog implements ILog {
public Dog(){
}
public String printLog(){
System.out.println("this is a Dog.");
return "end";
}
}
3、创建实现InvocationHandler接口的类(通过反射实现)
public class ProxyInvocationHandler implements InvocationHandler {
Object target;
public ProxyInvocationHandler(Object obj) {
target = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before " + method.getName());
Object result = method.invoke(target, args);
System.out.println("after " + method.getName());
return "invoke " + result;
}
}
4、创建代理类用以获取Demo类的对象
public class DynamicProxy {
public static Object getProxyObject() throws Exception {
ProxyInvocationHandler handler = new ProxyInvocationHandler(new Dog());
Class c = Proxy.getProxyClass(clazz.getClassLoader(), ILog.class);
Constructor con = c.getConstructor(InvocationHandler.class);
Object o = con.newInstance(handler);
return o;
}
}
5、测试类
public static void main(String[] args) throws Exception {
ILog dem = (ILog) getProxyObject();
System.out.println(dem.printLog());
}
总结:
优点:
1、职责清晰。2、高扩展性。3、智能化
缺点:
1)、由于在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请
求的处理速度变慢。
2)、实现代理模式需要额外的工作,有些代理模式的实现非常复杂。
项目中的使用场景:AOP面向切面