经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式六大原则:接口隔离原则
来源:cnblogs  作者:酷学大叔  时间:2019/9/10 10:57:41  对本文有异议

目录: 

  设计模式六大原则:单一职责原则

  设计模式六大原则:接口隔离原则 

  设计模式六大原则:依赖倒置原则

  设计模式六大原则:里氏替换原则

  设计模式六大原则:迪米特法则

  设计模式六大原则:开闭原则

接口隔离原则(Interface Segregation Principle):

  1、客户端不应依赖它不需要的接口

  2、类间的依赖关系应该建立在最小的接口上

  其实通俗来理解就是,不要在一个接口里面放很多的方法,这样会显得这个类很臃肿。接口应该尽量细化,一个接口对应一个功能模块,同时接口里面的方法应该尽可能的少,使接口更加灵活轻便。或许有的人认为接口隔离原则和单一职责原则很像,但两个原则还是存在着明显的区别。单一职责原则是在业务逻辑上的划分,注重的是职责。接口隔离原则是基于接口设计考虑。例如一个接口的职责包含10个方法,这10个方法都放在同一接口中,并且提供给多个模块调用,但不同模块需要依赖的方法是不一样的,这时模块为了实现自己的功能就不得不实现一些对其没有意义的方法,这样的设计是不符合接口隔离原则的。接口隔离原则要求"尽量使用多个专门的接口"专门提供给不同的模块。

经典案例:

  类A通过Interface1依赖类B,1,2,3;类B通过Interface1依赖D,1,4,5。

  

  1. 1 internal class Program
  2. 2 {
  3. 3 private static void Main(string[] args)
  4. 4 {
  5. 5 A a = new A();
  6. 6 B b = new B();
  7. 7 a.use1(b);
  8. 8 a.use2(b);
  9. 9 a.use3(b);
  10. 10 }
  11. 11 }
  12. 12
  13. 13 internal interface interface1
  14. 14 {
  15. 15 void operation1();
  16. 16
  17. 17 void operation2();
  18. 18
  19. 19 void operation3();
  20. 20
  21. 21 void operation4();
  22. 22
  23. 23 void operation5();
  24. 24 }
  25. 25
  26. 26 internal class B : interface1
  27. 27 {
  28. 28 public void operation1()
  29. 29 {
  30. 30 Console.WriteLine($"B->{nameof(operation1)}");
  31. 31 }
  32. 32
  33. 33 public void operation2()
  34. 34 {
  35. 35 Console.WriteLine($"B->{nameof(operation2)}");
  36. 36 }
  37. 37
  38. 38 public void operation3()
  39. 39 {
  40. 40 Console.WriteLine($"B->{nameof(operation3)}");
  41. 41 }
  42. 42
  43. 43 public void operation4()
  44. 44 {
  45. 45 Console.WriteLine($"B->{nameof(operation4)}");
  46. 46 }
  47. 47
  48. 48 public void operation5()
  49. 49 {
  50. 50 Console.WriteLine($"B->{nameof(operation5)}");
  51. 51 }
  52. 52 }
  53. 53
  54. 54 internal class D : interface1
  55. 55 {
  56. 56 public void operation1()
  57. 57 {
  58. 58 Console.WriteLine($"D->{nameof(operation1)}");
  59. 59 }
  60. 60
  61. 61 public void operation2()
  62. 62 {
  63. 63 Console.WriteLine($"D->{nameof(operation2)}");
  64. 64 }
  65. 65
  66. 66 public void operation3()
  67. 67 {
  68. 68 Console.WriteLine($"D->{nameof(operation3)}");
  69. 69 }
  70. 70
  71. 71 public void operation4()
  72. 72 {
  73. 73 Console.WriteLine($"D->{nameof(operation4)}");
  74. 74 }
  75. 75
  76. 76 public void operation5()
  77. 77 {
  78. 78 Console.WriteLine($"D->{nameof(operation5)}");
  79. 79 }
  80. 80 }
  81. 81
  82. 82 internal class A
  83. 83 {
  84. 84 public void use1(interface1 interface1)
  85. 85 {
  86. 86 interface1.operation1();
  87. 87 }
  88. 88
  89. 89 public void use2(interface1 interface1)
  90. 90 {
  91. 91 interface1.operation2();
  92. 92 }
  93. 93
  94. 94 public void use3(interface1 interface1)
  95. 95 {
  96. 96 interface1.operation3();
  97. 97 }
  98. 98 }
  99. 99
  100. 100 internal class C
  101. 101 {
  102. 102 public void use1(interface1 interface1)
  103. 103 {
  104. 104 interface1.operation1();
  105. 105 }
  106. 106
  107. 107 public void use4(interface1 interface1)
  108. 108 {
  109. 109 interface1.operation4();
  110. 110 }
  111. 111
  112. 112 public void use5(interface1 interface1)
  113. 113 {
  114. 114 interface1.operation5();
  115. 115 }
  116. 116 }
view code

  显然,上述设计不符合接口隔离原则。

  

  1. 1 internal class Program
  2. 2 {
  3. 3 private static void Main(string[] args)
  4. 4 {
  5. 5 A a = new A();
  6. 6 B b = new B();
  7. 7 a.use1(b);
  8. 8 a.use2(b);
  9. 9 a.use3(b);
  10. 10 }
  11. 11 }
  12. 12
  13. 13 internal interface interface1
  14. 14 {
  15. 15 void operation1();
  16. 16 }
  17. 17
  18. 18 internal interface interface2
  19. 19 {
  20. 20 void operation2();
  21. 21
  22. 22 void operation3();
  23. 23 }
  24. 24
  25. 25 internal interface interface3
  26. 26 {
  27. 27 void operation4();
  28. 28
  29. 29 void operation5();
  30. 30 }
  31. 31
  32. 32 internal class B : interface1, interface2
  33. 33 {
  34. 34 public void operation1()
  35. 35 {
  36. 36 Console.WriteLine($"B->{nameof(operation1)}");
  37. 37 }
  38. 38
  39. 39 public void operation2()
  40. 40 {
  41. 41 Console.WriteLine($"B->{nameof(operation2)}");
  42. 42 }
  43. 43
  44. 44 public void operation3()
  45. 45 {
  46. 46 Console.WriteLine($"B->{nameof(operation3)}");
  47. 47 }
  48. 48 }
  49. 49
  50. 50 internal class D : interface1, interface3
  51. 51 {
  52. 52 public void operation1()
  53. 53 {
  54. 54 Console.WriteLine($"D->{nameof(operation1)}");
  55. 55 }
  56. 56
  57. 57 public void operation4()
  58. 58 {
  59. 59 Console.WriteLine($"D->{nameof(operation4)}");
  60. 60 }
  61. 61
  62. 62 public void operation5()
  63. 63 {
  64. 64 Console.WriteLine($"D->{nameof(operation5)}");
  65. 65 }
  66. 66 }
  67. 67
  68. 68 internal class A
  69. 69 {
  70. 70 public void use1(interface1 interface1)
  71. 71 {
  72. 72 interface1.operation1();
  73. 73 }
  74. 74
  75. 75 public void use2(interface2 interface2)
  76. 76 {
  77. 77 interface2.operation2();
  78. 78 }
  79. 79
  80. 80 public void use3(interface2 interface2)
  81. 81 {
  82. 82 interface2.operation3();
  83. 83 }
  84. 84 }
  85. 85
  86. 86 internal class C
  87. 87 {
  88. 88 public void use1(interface1 interface1)
  89. 89 {
  90. 90 interface1.operation1();
  91. 91 }
  92. 92
  93. 93 public void use4(interface3 interface3)
  94. 94 {
  95. 95 interface3.operation4();
  96. 96 }
  97. 97
  98. 98 public void use5(interface3 interface3)
  99. 99 {
  100. 100 interface3.operation5();
  101. 101 }
  102. 102 }
view code

原文链接:http://www.cnblogs.com/az4215/p/11481076.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号