Design Patterns - Interface Segregation Principle seven principles of (E)

Definition of the interface segregation principle of

  Interface Segregation Principle (Interface Segregation Principle, ISP) require the programmer to try to split the bloated interface into smaller and more specific interface that allows interface methods contain only interested customers.

  2002 · C Robert Martin definition of "Interface Segregation Principle" is: The client should not be forced to rely on methods that do not use (Clients should not be forced to depend on methods they do not use). This principle has another definition: a class dependent on another class should be based on the smallest interface (The dependency of one class to another one should depend on the smallest possible interface).

  Two meanings defined above are: To establish a dedicated interface they need for each class, rather than trying to build a very large interface for all classes that depend on it to call.

  Interface segregation principle and single responsibility is to improve the cohesion and the like, to reduce the coupling between them, embodies the idea of the package, but they are different:

    • Single responsibility principle focus is duty, and the interface segregation principle focus is to isolate the interface dependent.
    • Single Responsibility Principle is the main constraint class, which is aimed at program implementation and details; Interface Segregation Principle The main constraint interfaces, the main building for the abstract and the overall framework of the program.

Isolation principle advantage of the interface

  Interface segregation principle in order to constrain the interface, reducing the dependence on the class interface, the interface follow the segregation principle has the following five advantages.

    1. The bloated interface divided into multiple small size interface, it can prevent the spread of exotic change, improve flexibility, and maintainability.
    2. Interface Isolation improved cohesion of the system, reducing the external interaction, reducing the coupling system.
    3. If the particle size defined interfaces reasonable, to ensure the stability of the system; however, if the definition is too small, it will cause excessive number of interfaces, complicating the design; If the definition is too large, reduce flexibility, unable to provide customized services, to overall project with unpredictable risks.
    4. Using multiple specialized interface also can reflect the level of objects, because it can through inheritance interfaces, to achieve the overall definition of the interface.
    5. You can reduce code redundancy project engineering. Interface oversized large number of methods which are usually placed not in use, when the implementation of this interface, forced to design redundant code.

Implementation of the Interface Segregation Principle

  When Interface Segregation Principle specific application, should be judged according to the following rules.

    • Interface as small as possible, but there's a limit. An interface dedicated to a single submodule or business logic.
    • Customized services to dependent interface. Only the caller needs to provide a method of shielding unwanted method.
    • Understanding of the environment, refused to follow blindly. Each project or product has selected environmental factors, different environments, different split of a standard interface to understand the business logic.
    • Improve cohesion and reduce the external interaction. The interfaces with the least method to accomplish most things.


Below student performance management program as an example application interface introduce the principle of isolation.

[Example 1] student achievement management program.

  Analysis: Student performance management program typically includes insertion results, delete results, modify the results to calculate the total score, calculated average print performance information, query performance information and other functions, if all of these functions into one interface obviously not reasonable , the correct way is to place three modules respectively input module, a statistics module and a print module, which is shown in Figure 1 class.

           Class Diagram student achievement management program
                      Class Diagram 1 student achievement management procedures Figure

Code is as follows:

  1. package principle;
  2. public class ISPtest{
  3.     public static void main(String[] args)  {
  4.         InputModule input =StuScoreList.getInputModule();
  5.         CountModule count =StuScoreList.getCountModule();
  6.         PrintModule print =StuScoreList.getPrintModule();
  7.         input.insert();
  8.         count.countTotalScore();
  9.         print.printStuInfo();
  10.         //print.delete();
  11.     }
  12. }
  13. // input module interface
  14. interface InputModule{
  15.       void insert();
  16.       void delete();
  17.       void modify();
  18. }
  19. // Statistics module interface
  20. interface CountModule{
  21.     void countTotalScore();
  22.     void countAverage();
  23. }
  24. // print module interface
  25. interface PrintModule{
  26.     void printStuInfo();
  27.     void queryStuInfo();
  28. }
  29. // implementation class
  30. class StuScoreList implements InputModule,CountModule,PrintModule{
  31.     private StuScoreList(){}
  32.     public static InputModule getInputModule()
  33.     {
  34.         return (InputModule)new StuScoreList();
  35.     }
  36.     public static CountModule getCountModule()
  37.     {
  38.         return (CountModule)new StuScoreList();
  39.     }
  40.     public static PrintModule getPrintModule()
  41.     {
  42.         return (PrintModule)new StuScoreList();
  43.     }
  44.     public void insert()
  45.     {
  46.         The System .out . The println ( "input module insert () method is called!" );
  47.     }
  48.     public void delete()
  49.     {
  50.         System the .out . Println ( "input module delete () method is called!" );
  51.     }
  52.     public void modify()
  53.     {
  54.         The System .out . The println ( "input module Modify () method is called!" );
  55.     }
  56.     public void countTotalScore()
  57.     {
  58.         System the .out . Println ( "statistics module countTotalScore () method is called!" );
  59.     }
  60.     public void countAverage()
  61.     {
  62.         System the .out . Println ( "statistics module countAverage () method is called!" );
  63.     }
  64.     public void printStuInfo()
  65.     {
  66.         System the .out . Println ( "print module printStuInfo () method is called!" );
  67.     }
  68.     public void queryStuInfo()
  69.     {
  70.         System the .out . Println ( "print module queryStuInfo () method is called!" );
  71.     }
  72. }


  Operating results of the program are as follows:

Enter insert module () method is called! 
CountTotalScore statistics module () method is called! 
Print printStuInfo module () method is called!

Guess you like

Origin www.cnblogs.com/yuexiaoyun/p/11825549.html