客户端不应该依赖他不需要的接口,即一个类对另一个类的依赖应该建立在最小接口上

    1. package com.cedric.segregation;
    2. // 没有使用接口隔离原则
    3. /*
    4. 客户端不应该依赖他不需要的接口,即一个类对另一个类的依赖应该建立在最小接口上
    5. Interface1接口对于A类和C类来说不是最小最小接口,A类和C类不需要实现多余的方法
    6. 隔离原则处理:
    7. 将接口Interface1拆分成几个独立接口,A类和C类分别与他们需要的接口建立关系,采用接口隔离原则
    8. */
    9. public class SegregationDemo01 {
    10. public static void main(String[] args) {
    11. }
    12. }
    13. // Interface1接口
    14. interface Interface1{
    15. void operation1();
    16. void operation2();
    17. void operation3();
    18. void operation4();
    19. void operation5();
    20. }
    21. // 实现类B
    22. class B implements Interface1{
    23. @Override
    24. public void operation1() {
    25. System.out.println("B类 --> operation1");
    26. }
    27. @Override
    28. public void operation2() {
    29. System.out.println("B类 --> operation2");
    30. }
    31. @Override
    32. public void operation3() {
    33. System.out.println("B类 --> operation3");
    34. }
    35. @Override
    36. public void operation4() {
    37. System.out.println("B类 --> operation4");
    38. }
    39. @Override
    40. public void operation5() {
    41. System.out.println("B类 --> operation5");
    42. }
    43. }
    44. // 实现类D
    45. class D implements Interface1{
    46. @Override
    47. public void operation1() {
    48. System.out.println("D类 --> operation1");
    49. }
    50. @Override
    51. public void operation2() {
    52. System.out.println("D类 --> operation2");
    53. }
    54. @Override
    55. public void operation3() {
    56. System.out.println("D类 --> operation3");
    57. }
    58. @Override
    59. public void operation4() {
    60. System.out.println("D类 --> operation4");
    61. }
    62. @Override
    63. public void operation5() {
    64. System.out.println("D类 --> operation5");
    65. }
    66. }
    67. class A{ // A类通过接口Interface1 依赖(使用)B类 但是只会用到1,2,3方法
    68. public void depend1(Interface1 i){
    69. i.operation1();
    70. }
    71. public void depend2(Interface1 i){
    72. i.operation2();
    73. }
    74. public void depend(Interface1 i){
    75. i.operation3();
    76. }
    77. }
    78. class C{ // B类通过接口Interface1使用D类 但是只会用到1,4,5方法
    79. public void depend1(Interface1 i){
    80. i.operation1();
    81. }
    82. public void depend4(Interface1 i){
    83. i.operation4();
    84. }
    85. public void depend5(Interface1 i){
    86. i.operation5();
    87. }
    88. }

    image.png

    package com.cedric.segregation;
    
    /*
    改进方案:
        采用接口隔离原则,使用最小接口
     */
    public class SegregationDemo02 {
        public static void main(String[] args) {
            A1 a1 = new A1();
            a1.depend1(new B1()); // A类通过接口依赖B类
            a1.depend2(new B1());
    
            C1 c = new C1();
            c.depend1(new D1()); // C类通过接口依赖D类
            c.depend2(new D1());
        }
    }
    
    interface Interface01{
        void operation1();
    }
    
    interface Interface02{
        void operation2();
        void operation3();
    }
    
    interface Interface03{
        void operation4();
        void operation5();
    }
    
    class B1 implements Interface01,Interface02{
    
        @Override
        public void operation1() {
            System.out.println("B1==>operation01");
        }
    
        @Override
        public void operation2() {
            System.out.println("B1==>operation02");
        }
    
        @Override
        public void operation3() {
            System.out.println("B1==>operation03");
        }
    }
    
    class D1 implements Interface01,Interface03{
    
        @Override
        public void operation1() {
            System.out.println("D1 ==> operation01");
        }
    
        @Override
        public void operation4() {
            System.out.println("D1 ==> operation04");
        }
    
        @Override
        public void operation5() {
            System.out.println("D1 ==> operation05");
        }
    }
    
    class A1{
        public void depend1(Interface01 i1){
            i1.operation1();
        }
    
        public void depend2(Interface02 i2){
            i2.operation2();
            i2.operation3();
        }
    }
    
    class C1{
        public void depend1(Interface01 i1){
            i1.operation1();
        }
    
        public void depend2(Interface03 i3){
            i3.operation4();
            i3.operation5();
        }
    }