Design Mode - Interface Software Design Principles 4- isolation principle

Interface segregation principle (InterfaceSegregationPrinciple, ISP) refers to the use of a plurality of dedicated interfaces, interfaces without using the single general, the client should not rely on it does not interface. The principles that guide us in the design of the interface should note the following points:
1, a class dependent on a class should be based on a minimum of interfaces.
2, the establishment of a single interface, do not create bloated interface.
3, an interface as possible refinement process to minimize interface (not better, must be appropriate).
Interface segregation principle in line with our design philosophy often say high cohesion and low coupling, so that the class has good readability, scalability and maintainability. We have to think about the design of the interface of the time, spend time, to consider the business model, including with the potential to change the place of occurrence need to do some pre-judgment. So, for abstract understanding of the business model it is very important. Here we look at a piece of code, write a animal behavior Abstract: IAnimal Interface:

1 package com.lch.test.interfacesegregation;
2 
3 public interface IAnimal {
4     void eat();
5     void fly();
6     void swim();
7 }

As can be seen, Bird's swim () method may only be empty, Dog's fly () method is obviously impossible. At this time, we focused on different animal behavior to design different interfaces were designed IEatAnimal, IFlyAnimal and ISwimAnimal interfaces, look at the code:
IEatAnimal Interface:

package com.lch.test.interfacesegregation;

public interface IEatAnimal {
    void eat();
}
package com.lch.test.interfacesegregation;

public interface IFlyAnimal {
    void fly();
}
package com.lch.test.interfacesegregation;

public interface ISwimAnimal {
    void swim();
}

Dog only achieved IEatAnimal and ISwimAnimal interfaces: 

 1 package com.lch.test.interfacesegregation;
 2 
 3 public class Dog implements IEatAnimal,ISwimAnimal {
 4 
 5     @Override
 6     public void eat() {
 7 
 8     }
 9 
10     @Override
11     public void swim() {
12 
13     }
14 }

Bird achieved only eat and fly interface

 1 package com.lch.test.interfacesegregation;
 2 
 3 import jdk.nashorn.internal.ir.IfNode;
 4 
 5 public class Bird implements IEatAnimal, IFlyAnimal {
 6     @Override
 7     public void eat() {
 8     }
 9 
10     @Override
11     public void fly() {
12     }
13 }

FIG Comparative UML class as follows:

 

Guess you like

Origin www.cnblogs.com/enjoyjava/p/11334963.html