定义:为其他对象提供一种代理以控制对这个对象的访问。
实现组成:
-
抽象角色:通过接口或抽象类声明真实角色实现的业务方法。
-
代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。
-
真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用
两种实现方式:静态代理、动态代理
静态代理模拟的代码:
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 class Test {
public static void main(String[] args) {
IDemo demo = new StaticProxy(new Demo());
System.out.println(demo.print());
}
}
动态代理模拟的代码:
1、抽象角色(代理实现的内容)
public interface Movable{
void move();
}
2、真实角色(被代理的类)
public class Car implements Movable {
public void move() {
System.out.println("Tank moving claclacla..."+this.getClass().getName());
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Plane implements Movable {
public void move() {
System.out.println("Tank moving claclacla..."+this.getClass().getName());
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3、创建实现代理的类(通过反射实现)
class TimeProxy implements InvocationHandler {
Movable m;
public TimeProxy(Movable m) {
this.m = m;
}
public void before() {
System.out.println("method start.."+m.getClass().getName());
}
public void after() {
System.out.println("method stop.."+m.getClass().getName());
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object o = method.invoke(m, args);
after();
return o;
}
}
4、测试类
public class Test {
public static void main(String[] args) {
Movable m = (Movable) Proxy.newProxyInstance(
Car.class.getClassLoader(), new Class[] { Movable.class },
new TimeProxy(new Car()));
m.move();
Movable m2 = (Movable) Proxy.newProxyInstance(
Car.class.getClassLoader(), new Class[] { Movable.class },
new TimeProxy(new Plane()));
m2.move();
}
}
总结:
优点:
1、职责清晰。2、高扩展性。3、智能化
缺点:
1)、由于在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请
求的处理速度变慢。
2)、实现代理模式需要额外的工作,有些代理模式的实现非常复杂。
项目中的使用场景:AOP面向切面