代理模式的定义:因为某些缘由须要给某对象提供一个代理以控制对该对象的访问。这时,访问对象不适合或者不能直接引用目标对象,代理对象做为访问对象和目标对象之间的中介。java
代理模式的主要优势有:
代理模式在客户端与目标对象之间起到一个中介做用和保护目标对象的做用;
代理对象能够扩展目标对象的功能;
代理模式能将客户端与目标对象分离,在必定程度上下降了系统的耦合度;spring
其主要缺点是:
在客户端和目标对象之间增长一个代理对象,会形成请求处理速度变慢;
增长了系统的复杂度;sql
代理模式的主要角色以下。
抽象主题(Subject)类:经过接口或抽象类声明真实主题和代理对象实现的业务方法。
真实主题(Real Subject)类:实现了抽象主题中的具体业务,是代理对象所表明的真实对象,是最终要引用的对象。
代理(Proxy)类:提供了与真实主题相同的接口,其内部含有对真实主题的引用,它能够访问、控制或扩展真实主题的功能。shell
在代理模式中动态代理是在各个框架中使用最普遍的一种设计模式,dubbo中 feign中 mybaits中,都有使用到动态代理。在dubbo中,在接口上添加@refrence ,dubbo就会根据这个接口生成一个代理实例来供消费者用生产者。在feign中也是同样;mybatis中你只要指定包扫描的路径,就会在spring中注入一个mapper,实际上这个mapper就是根据接口和xml生成的代理对象。实际上,这种“申明式的”功能实现方式,都是经过代理模式来实现的。设计模式
下面咱们经过cglib来写一个“残疾缩水”版的mybatis:安全
首先要整一个xml,我这里用properties代替服务器
test.properties:mybatis
testA=select * from user where id= testB=select * from user where username=
再整一个接口:app
interface Test { String testA(int id); String testB(String username); }
再是代码加强处理器,这里面完成对接口的代理逻辑框架
class MyInvokationHandler implements MethodInterceptor { private static Map<String,String> sqlMap; { HashMap<String, String> map = new HashMap<>(); Properties prop = new Properties(); InputStream in = SystemMenuController.class.getClassLoader().getResourceAsStream("test.properties"); try { prop.load(in); Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String key = it.next(); map.put(key,prop.getProperty(key)); } in.close(); } catch (java.io.IOException e) { e.printStackTrace(); } sqlMap=map; } @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { String name = method.getName(); String s = sqlMap.get(name); return s+objects[0]; } }
包扫描和启动时注入容器略,数据源也略,用个main方法模拟一下:
public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Test.class); enhancer.setCallback(new MyInvokationHandler()); Test test = (Test) enhancer.create(); System.out.println(test.testA(1)); System.out.println(test.testB("hhh")); }
最终在控制台中打印:
select * from user where id=1 select * from user where id=hhh
代理模式是一个很强大实用性很强的模式,动态代理大大减小了咱们的开发任务,同时减小了对业务接口的依赖,下降了耦合度。代理模式的使用场景能够总结为: