定义:给目标对象提供一个代理对象,并由代理对象控制对目标对象的引用

整个设计模式,我们需要对拓展开放,对修改关闭

目的:

  • 通过引入代理对象的方式来间接访问目标对象,防止直接访问目标对象给系统带来的不必要复杂性
  • 通过代理对象对原有的业务增强

真实对象:代理对象所代表的真实对象,最终被引用的对象

抽象对象:声明了真实对象和代理对象的公共接口

代理对象:包含真实对象从而操作真实主题对象,相当于访问与真实对象直接的中介

静态代理

整体结构

  1. //接口
  2. public interface MTFactory {
  3. void saleMT(String s);
  4. }
  5. //实现类
  6. public class AFactory implements MTFactory {
  7. @Override
  8. public void saleMT(String s) {
  9. System.out.println("AFactory 方法正在执行--"+s);
  10. }
  11. }
  12. //静态代理实现
  13. //扩展能力差
  14. //可维护性差
  15. public class StaticProxy implements MTFactory, WTFactory {
  16. //包含真实对象
  17. public MTFactory factory;
  18. public WTFactory wtFactory;
  19. public StaticProxy(MTFactory factory) {
  20. this.factory=factory;
  21. }
  22. public StaticProxy(WTFactory wtFactory) {
  23. this.wtFactory=wtFactory;
  24. }
  25. @Override
  26. public void saleMT(String s) {
  27. dosomeBefore();//前置增强
  28. factory.saleMT(s);
  29. dosomeEnd();//后置增强
  30. }
  31. private void dosomeEnd() {
  32. System.out.println("前置增强正在执行");
  33. }
  34. private void dosomeBefore() {
  35. System.out.println("后置增强在执行");
  36. }
  37. @Override
  38. public void saleWT(int l) {
  39. dosomeBefore();
  40. wtFactory.saleWT(2);
  41. dosomeEnd();
  42. }
  43. }
  44. //CLient
  45. public class Client {
  46. public static void main(String[] args) {
  47. MTFactory mtFactory=new AFactory();
  48. ShiXian shiXian = new ShiXian(mtFactory);
  49. shiXian.saleMT("D");
  50. }
  51. }

静态代理的时候,每当我们需要多一个需求的时候,就需要多一个接口,一个实现方法,然后具体引用这个方法的时候还需要更改。不便于拓展。

动态代理

动态代理核心:ProxyInvocationHandler

Proxy:proxy provides static methods for creating dynamic proxy classes and instance,and it is also the superclass of all dynamic proxy classes created by those method.

通过

  1. public static Object newProxyInstance(ClassLoader loader,
  2. Class<?>[] interfaces,
  3. InvocationHandler h)

动态生成代理对象

InvocationHandler:{@code InvocationHandler} is the interface implemented by the invocation handler of a proxy instance.

  1. public interface InvocationHandler {
  2. public Object invoke(Object proxy, Method method, Object[] args)
  3. throws Throwable;
  4. }
  1. public class Dynamic implements InvocationHandler {
  2. //绑定委托对象,并返回代理类
  3. private Object factory;
  4. public Object getFactory() {
  5. return factory;
  6. }
  7. public void setFactory(Object factory) {
  8. this.factory = factory;
  9. }
  10. //通过Proxy获取动态代理的对象,实现该类的所有接口并取得代理类
  11. public Object getProxyInstance() {
  12. return Proxy.newProxyInstance(factory.getClass().getClassLoader(), factory.getClass().getInterfaces(), this);
  13. }
  14. @Override
  15. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  16. dosomeBefore();
  17. //围绕这个方法就是AOP
  18. Object invoke = method.invoke(factory,args);
  19. dosomeEnd();
  20. //调用代理对象
  21. return invoke;
  22. }
  23. private void dosomeEnd() {
  24. System.out.println("前置增强正在执行");
  25. }
  26. private void dosomeBefore() {
  27. System.out.println("后置增强在执行");
  28. }
  29. }
  30. 执行
  31. public class Client {
  32. public static void main(String[] args) {
  33. //静态代理
  34. /* MTFactory mtFactory=new AFactory();
  35. StaticProxy shiXian = new StaticProxy(mtFactory);
  36. shiXian.saleMT("D");
  37. System.out.println("====================================");*/
  38. //动态代理
  39. MTFactory mtFactory=new AFactory();
  40. Dynamic dynamic =new Dynamic();
  41. dynamic.setFactory(mtFactory);
  42. MTFactory proxyInstance = (MTFactory) dynamic.getProxyInstance();
  43. proxyInstance.saleMT("D");
  44. System.out.println("============================");
  45. }
  46. }