1. Basic introduction

A client should not rely on interfaces it does not need, that is, the dependency of one class on another should be based on the smallest interface As can be seen from the figure:1. Class A relies on class B through Interface1, but only uses 1,2,3 methods. 2. Class C relies on d4 through the interface Interface1. If interface Interface1 is not the minimum interface for both classes A and C, then classes B and D must implement methods they do not need

According to the interface isolation principle:Split interface Interface1 into separate interfaces, with classes A and C dependent on the interfaces they need

2. Application cases

  • Code that does not use interface isolation

    public class Segregation1 {
        public static void main(String[] args) {}}/ / interface
    interface Interface1 {
        void operation1(a);
        void operation2(a);
        void operation3(a);
        void operation4(a);
        void operation5(a);
    }
    
    class B implements Interface1 {
        public void operation1(a) {
            System.out.println("B operation1");
        }
    
        public void operation2(a) {
            System.out.println("B operation2");
        }
        public void operation3(a) {
            System.out.println("B operation3");
        }
        public void operation4(a) {
            System.out.println("B operation4");
        }
        public void operation5(a) {
            System.out.println("B operation5"); }}class D implements Interface1 {
        public void operation1(a) {
            System.out.println("D operation1");
        }
    
        public void operation2(a) {
            System.out.println("D operation2");
        }
        public void operation3(a) {
            System.out.println("D operation3");
        }
        public void operation4(a) {
            System.out.println("D operation4");
        }
        public void operation5(a) {
            System.out.println("D operation5"); }}class A { // Class A relies on class B through the interface Interface1, but only the 1,2, and 3 methods are used
        public void depend1(Interface1 i) {
            i.operation1();
        }
        public void depend2(Interface1 i) {
            i.operation2();
        }
        public void depend3(Interface1 i) { i.operation3(); }}class C { // class C relies on class D through the interface Interface1, but only uses the 1,4,5 methods
        public void depend1(Interface1 i) {
            i.operation1();
        }
        public void depend4(Interface1 i) {
            i.operation4();
        }
        public void depend5(Interface1 i) { i.operation5(); }}Copy the code

    Because the interface isolation principle is not adopted, class A only needs to use methods 1, 2, and 3 in Interface1 through class B, but has to implement methods 4,5 in Interface1: Class C only needs to use the 1,4,5 methods of Interface1 through class D, but has to implement the 2,3 methods of the interface, which will cause too much redundant code, appear bloated class solution: Interface1(OPERATION1), Interface2(Operation2, Operation3), and Interface3(Operation4, Operation5) implement their interfaces according to their requirements

  • Code that uses interface isolation

    public class Segregation1 {
        public static void main(String[] args) {
            A a = new A();
            a.depend1(new B()); // Class A relies on class B through interfaces
            a.depend2(new B());
            a.depend3(new B());
    
            C c = new C();
            c.depend1(new D()); // Class C relies on class D through interfaces
            c.depend4(new D());
            c.depend5(newD()); }}1 / / interface
    interface Interface1 {
        void operation1(a);
    }
    
    2 / / interface
    interface Interface2 {
        void operation2(a);
        void operation3(a);
    }
    
    3 / / interface
    interface Interface3 {
        void operation4(a);
        void operation5(a);
    }
    
    class B implements Interface1.Interface2 {
        public void operation1(a) {
            System.out.println("B operation1");
        }
    
        public void operation2(a) {
            System.out.println("B operation2");
        }
    
        public void operation3(a) {
            System.out.println("B operation3"); }}class D implements Interface1.Interface3 {
        public void operation1(a) {
            System.out.println("D operation1");
        }
    
        public void operation4(a) {
            System.out.println("D operation4");
        }
    
        public void operation5(a) {
            System.out.println("D operation5"); }}class A { // Class A relies on class B through the interface Interface1, which uses only the 1,2, and 3 methods
        public void depend1(Interface1 i) {
            i.operation1();
        }
    
        public void depend2(Interface2 i) {
            i.operation2();
        }
    
        public void depend3(Interface2 i) { i.operation3(); }}class C { // class C uses the interface Interface1, which relies on class D, but only uses the 1,4,5 methods
        public void depend1(Interface1 i) {
            i.operation1();
        }
    
        public void depend4(Interface3 i) {
            i.operation4();
        }
    
        public void depend5(Interface3 i) { i.operation5(); }}Copy the code

3. Notes and details

  1. A client should not rely on interfaces it does not need
  2. Dependencies between classes should be established on the smallest interface
  3. Don’t put too many methods in an interface, as this will make the class look bloated. Interfaces should be as detailed as possible, one interface corresponds to one function module, and the methods in the interface should be as few as possible to make the interface more portable and flexible
  4. The principle of single responsibility requires a single class and interface responsibility, focusing on responsibility and division of business logic, while the principle of interface isolation requires as few methods as possible, which is considered in interface design. For example, an interface contains 10 method, the duty of the 10 methods in an interface, and provides access to multiple modules, each module according to the limits of their authority to access, and specifies “do not use the method cannot access”, such a design is not in conformity with the interface segregation principle, interface segregation principle request “as far as possible to use multiple special interface”, Specialized interfaces here mean that each module should be provided with a single interface (that is, one interface for each module), rather than creating a large, bloated interface to accommodate all client access