代理模式
代理是最基本的设计模式之一,在SpringAOP中非常常见,也是面试中必问的一项。代理为你提供了额外的或不同的操作,而插入的用来代替 “实际”对象的对象。这些操作通常涉及与“实例”对象的通信,因此代理通常充当中间人的角色。
静态代理
//定义代理接口
public interface Interface {
//定义两个方法
void doSomething();
void somethingElse(String message);
}
//实际类
public class RealObject implements Interface{
@Override
public void doSomething() {
System.out.println("doSomething without message");
}
@Override
public void somethingElse(String message) {
System.out.println("dosomething with message : "+message);
}
}
//简单代理类
public class SimpleProxy implements Interface {
private Interface proxy;
//利用构造器代理一个或多个类
public SimpleProxy(Interface proxy) {
this.proxy=proxy;
}
public void doSomething(){
System.out.println("SimpleProxy doSomething");
proxy.doSomething();
}
public void somethingElse(String message){
System.out.println("SimpleProxy doSomething with message : "+message);
proxy.somethingElse(message);
}
}
//代理测试类
public class SimpleProxyDemo {
public static void main(String[] args) {
consumer(new RealObject());//代理RealObject类
consumer(new SimpleProxy(new RealObject()));//代理SimpleProxy类
}
//封装一个方法来管理代理方法
public static void consumer(Interface inter){
inter.doSomething();
inter.somethingElse("pingpong");
}
}
---------------------------console输出--------------------------------------------
doSomething without message
dosomething with message : pingpong
SimpleProxy doSomething
doSomething without message
SimpleProxy doSomething with message : pingpong
dosomething with message : pingpong
因为consumer()接受Interface,所以它无法知道正在获得的到底是RealObject还是SimpleProxy,因为二者都实现了Interface。但是SimpleProxy已经被插入到客户端和RealObject中间,因此他会执行操作,然后调用RealObject上相同的方法。
动态代理
Java的动态代理比代理在思想上更进了一步,因为他可以动态的创建代理并动态的处理对代理方法的调用。在动态代理上所做的所有调用都会被重定向到单一的调用处理器上,他的工作是揭示调用的类型并确定相应的对策。
在实现动态代理的时候,要实现以下接口和方法:
实现InvocationHandler接口
调用Proxy.newIProxynstance(ClassLoader loader,Class<?>[]interfaces,InvocationHandler h);
ClassLoader loader:JVM自动生成的代理对象
Class<?>[]interfaces :当前代理对象调用的方法对象,是代理对象实现接口中的方法
InvocationHander h :调用对象传入的参数(handler类利用构造方法传入被代理类)
//定义代理接口
public interface Interface {
//定义两个方法
void doSomething();
void somethingElse(String message);
}
//被代理类
public class RealObject implements Interface{
@Override
public void doSomething() {
System.out.println("doSomething without message");
}
@Override
public void somethingElse(String message) {
System.out.println("dosomething with message : "+message);
}
}
//
public class DynamicProxyHandler implements InvocationHandler {
//真实的对象
private Object proxied;
public DynamicProxyHandler(Object proxied) {
this.proxied=proxied;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("*** proxy : "+proxy.getClass()+" , method : "+method+" ,args : "+args);
return method.invoke(proxied, args);//自动调用代理目标(RealObject)的方法(利用方法的反射)
}
}
public class DynamicSimpleProxy {
public static void main(String[] args) {
RealObject real=new RealObject();
consumer(real);//未被动态调用,实现静态代理
Interface proxy=(Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
new Class[]{Interface.class}, new DynamicProxyHandler(real));
consumer(proxy);//通过Interface类的对象proxy向下进行动态调用
}
public static void consumer(Interface inte){
inte.doSomething();
inte.somethingElse("pingpong");
}
}
-------------------------------------console输出-------------------------------------
doSomething without message
dosomething with message : pingpong
*** proxy : class com.sun.proxy.$Proxy0 , method : public abstract void proxymodel.Interface.doSomething() ,args : null
doSomething without message
*** proxy : class com.sun.proxy.$Proxy0 , method : public abstract void proxymodel.Interface.somethingElse(java.lang.String) ,args : [Ljava.lang.Object;@6bc7c054
dosomething with message : pingpong