代理模式之静态代理

忘是亡心i 2024-04-07 15:31 146阅读 0赞

代理模式之静态代理

1.代理模式的概念

二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。让不属于目标方法核心逻辑的代码从目标方法中剥离出来——解耦。调用目标方法时先调用代理对象的方法,减少对目标方法的调用和打扰,同时让附加功能能够集中在一起也有利于统一维护。

个人理解:就是在一些方法实现中一些功能比如带日志的实现类,需要在类中实现日志输出的代码。它对核心业务功能有干扰,分散在各个业务功能方法中,不利于统一的封装和维护。 使用代理模式通过代理类来实现这些分散的方法并调用目标方法

在这里插入图片描述在使用代理模式后:
在这里插入图片描述
以下为具体的代码实现:
这里以实现四种基本计算方法为例,通过代理类将日志输出与核心代码方法实现返回

  1. package com.qcw.spring.proxy;
  2. public interface Calculator {
  3. int add(int i, int j);
  4. int sub(int i, int j);
  5. int mul(int i, int j);
  6. int div(int i, int j);
  7. }
  8. package com.qcw.spring.proxy;
  9. public class CalculatorImpl implements Calculator{
  10. public int add(int i, int j) {
  11. int result = i + j;
  12. System.out.println("方法内部,result"+ result);
  13. return result;
  14. }
  15. public int sub(int i, int j) {
  16. int result = i - j;
  17. System.out.println("方法内部,result"+ result);
  18. return result;
  19. }
  20. public int mul(int i, int j) {
  21. int result = i * j;
  22. System.out.println("方法内部,result"+ result);
  23. return result;
  24. }
  25. public int div(int i, int j) {
  26. int result = i / j;
  27. System.out.println("方法内部,result"+ result);
  28. return result;
  29. }
  30. }
  31. package com.qcw.spring.proxy;
  32. public class CalculatorStaticProxy implements Calculator {
  33. private CalculatorImpl target;
  34. //创建有参构造能够在类中调用方法
  35. public CalculatorStaticProxy(CalculatorImpl target) {
  36. this.target = target;
  37. }
  38. public int add(int i, int j) {
  39. System.out.println("日志,方法:add,参数:"+i+","+j);
  40. int result = target.add(i, j);
  41. System.out.println("日志,方法:add,结果:"+result);
  42. return result;
  43. }
  44. public int sub(int i, int j) {
  45. System.out.println("日志,方法:sub,参数:"+i+","+j);
  46. int result = target.sub(i, j);
  47. System.out.println("日志,方法:sub,结果:"+result);
  48. return result;
  49. }
  50. public int mul(int i, int j) {
  51. System.out.println("日志,方法:mul,参数:"+i+","+j);
  52. int result = target.mul(i, j);
  53. System.out.println("日志,方法:mul,结果:"+result);
  54. return result;
  55. }
  56. public int div(int i, int j) {
  57. System.out.println("日志,方法:div,参数:"+i+","+j);
  58. int result = target.div(i, j);
  59. System.out.println("日志,方法:div,结果:"+result);
  60. return result;
  61. }
  62. }
  63. package com.qcw.spring.test;
  64. import com.qcw.spring.proxy.CalculatorImpl;
  65. import com.qcw.spring.proxy.CalculatorStaticProxy;
  66. import org.junit.Test;
  67. public class CalculatorTest {
  68. @Test
  69. public void testCalculator(){
  70. CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());
  71. proxy.add(1,2);
  72. }
  73. }

发表评论

表情:
评论列表 (有 0 条评论,146人围观)

还没有评论,来说两句吧...

相关阅读

    相关 代理模式静态代理

    代理模式之静态代理 1.代理模式的概念 二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标方法的时候,不再是直接对目标方法进

    相关 代理模式-静态代理

    代理模式是指,为其他对象提供一种代理以控制这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户类和目标对象之间起到中介的作用。 换句