当前位置:   article > 正文

设计模式-接口隔离原则

设计模式-接口隔离原则

基本介绍

  1. 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
  2. 先看一张图:

  1. 类A通过接口Interface1 依赖类B,类C通过接口Interface1 依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。
  2. 按隔离原则应当这样处理:

将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则

传统代码

  1. /**
  2. * @author zhupanlin
  3. * @version 1.0
  4. * @description: TODO
  5. * @date 2024/4/9 10:47
  6. */
  7. public class Segregation1 {
  8. public static void main(String[] args) {
  9. }
  10. }
  11. interface Interface1 {
  12. void operation1();
  13. void operation2();
  14. void operation3();
  15. void operation4();
  16. void operation5();
  17. }
  18. class B implements Interface1{
  19. @Override
  20. public void operation1() {
  21. System.out.println("B 实现了 operation1");
  22. }
  23. @Override
  24. public void operation2() {
  25. System.out.println("B 实现了 operation2");
  26. }
  27. @Override
  28. public void operation3() {
  29. System.out.println("B 实现了 operation3");
  30. }
  31. @Override
  32. public void operation4() {
  33. System.out.println("B 实现了 operation4");
  34. }
  35. @Override
  36. public void operation5() {
  37. System.out.println("B 实现了 operation5");
  38. }
  39. }
  40. class D implements Interface1{
  41. @Override
  42. public void operation1() {
  43. System.out.println("D 实现了 operation1");
  44. }
  45. @Override
  46. public void operation2() {
  47. System.out.println("D 实现了 operation2");
  48. }
  49. @Override
  50. public void operation3() {
  51. System.out.println("D 实现了 operation3");
  52. }
  53. @Override
  54. public void operation4() {
  55. System.out.println("D 实现了 operation4");
  56. }
  57. @Override
  58. public void operation5() {
  59. System.out.println("D 实现了 operation5");
  60. }
  61. }
  62. // A类通过接口Interface 依赖(使用)B类,但是只会用到1,2,3方法
  63. class A {
  64. public void depend1(Interface1 i){
  65. i.operation1();
  66. }
  67. public void depend2(Interface1 i){
  68. i.operation2();
  69. }
  70. public void depend3(Interface1 i){
  71. i.operation3();
  72. }
  73. }
  74. // C类通过接口Interface 依赖(使用)D类,但是只会用到1,4,5方法
  75. class C{
  76. public void depend1(Interface1 i){
  77. i.operation1();
  78. }
  79. public void depend4(Interface1 i){
  80. i.operation4();
  81. }
  82. public void depend5(Interface1 i){
  83. i.operation5();
  84. }
  85. }

应传统方法的问题和使用接口隔离原则改进

类A通过接口Interface1依赖类B,类C通过接口Interface1 依赖类D,如果接口Interface1 对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法

将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则

接口Interface1中出现的方法,根据实际情况拆分为三个接口

代码示例:

  1. /**
  2. * @author zhupanlin
  3. * @version 1.0
  4. * @description: TODO
  5. * @date 2024/4/9 10:47
  6. */
  7. public class Segregation1 {
  8. public static void main(String[] args) {
  9. }
  10. }
  11. interface Interface1 {
  12. void operation1();
  13. void operation2();
  14. void operation3();
  15. void operation4();
  16. void operation5();
  17. }
  18. class B implements Interface1{
  19. @Override
  20. public void operation1() {
  21. System.out.println("B 实现了 operation1");
  22. }
  23. @Override
  24. public void operation2() {
  25. System.out.println("B 实现了 operation2");
  26. }
  27. @Override
  28. public void operation3() {
  29. System.out.println("B 实现了 operation3");
  30. }
  31. @Override
  32. public void operation4() {
  33. System.out.println("B 实现了 operation4");
  34. }
  35. @Override
  36. public void operation5() {
  37. System.out.println("B 实现了 operation5");
  38. }
  39. }
  40. class D implements Interface1{
  41. @Override
  42. public void operation1() {
  43. System.out.println("D 实现了 operation1");
  44. }
  45. @Override
  46. public void operation2() {
  47. System.out.println("D 实现了 operation2");
  48. }
  49. @Override
  50. public void operation3() {
  51. System.out.println("D 实现了 operation3");
  52. }
  53. @Override
  54. public void operation4() {
  55. System.out.println("D 实现了 operation4");
  56. }
  57. @Override
  58. public void operation5() {
  59. System.out.println("D 实现了 operation5");
  60. }
  61. }
  62. // A类通过接口Interface 依赖(使用)B类,但是只会用到1,2,3方法
  63. class A {
  64. public void depend1(Interface1 i){
  65. i.operation1();
  66. }
  67. public void depend2(Interface1 i){
  68. i.operation2();
  69. }
  70. public void depend3(Interface1 i){
  71. i.operation3();
  72. }
  73. }
  74. // C类通过接口Interface 依赖(使用)D类,但是只会用到1,4,5方法
  75. class C{
  76. public void depend1(Interface1 i){
  77. i.operation1();
  78. }
  79. public void depend4(Interface1 i){
  80. i.operation4();
  81. }
  82. public void depend5(Interface1 i){
  83. i.operation5();
  84. }
  85. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/444352
推荐阅读
相关标签
  

闽ICP备14008679号