Examples illustrate the use of ISP interface isolation principle in Java design pattern programming _java

Source: Internet
Author: User

The Interface segregation Principle,isp interface Isolation principle advocates that it is better to use multiple specialized interfaces than to use a single total interface.
The dependency of a class on another class should be based on the smallest interface.
An interface represents a role and should not be assigned to an interface with different roles. Interfaces that do not have a relationship are merged to form a bloated, large interface that is polluting the roles and interfaces.
"Customers should not be forced to rely on methods they do not use." The interface belongs to the customer and does not belong to the class hierarchy in which it resides. "This makes sense, and, more generally, don't force customers to use the methods they're not using, and if you force users to use the methods they don't use, those customers will face changes due to changes in these unused methods," he said.

Use the occasion, provide the caller needs the method, shielding the unwanted method. Satisfy the principle of interface isolation. For example, e-commerce system, there are orders for this class, there are three places will be used,

    1. One is a portal, there are only query methods,
    2. One is the external system, there are ways to add orders,
    3. One is admin backend, add delete modify query to use.

Depending on the interface isolation principle (ISP), a class's dependency on another class should be based on the smallest interface.

That is, for a portal, it can only rely on an interface with a query method.
The UML structure is as follows:

Let's look at an example of a Java interface that ignores the principle of interface isolation:

Interface I {public void method1 (); 
  public void method2 (); 
  public void method3 (); 
  public void method4 (); 
public void Method5 (); 
  Class a{public void Depend1 (I i) {i.method1 (); 
  public void Depend2 (I i) {i.method2 (); 
  public void Depend3 (I i) {i.method3 (); 
  Class B implements i{public void Method1 () {System.out.println ("Class B Implementation Interface I, Method 1"); 
  public void Method2 () {System.out.println ("Class B Implementation Interface I, Method 2"); 
  public void Method3 () {System.out.println ("Class B Implementation Interface I, Method 3"); 
  //For Class B, method4 and METHOD5 are not required, but because of the two methods in interface A,//so in the implementation process even if the method body of the two methods is empty, the two methods do not have a role to implement. 
  public void Method4 () {} is public void Method5 () {}} class c{public void Depend1 (I i) {i.method1 (); 
  public void Depend2 (I i) {i.method4 (); 
  public void Depend3 (I i) {i.method5 (); 
  Class D implements i{public void Method1 () {System.out.println ("Class D Implementation Interface I, Method 1"); }//For Class D, method2 and method3 are not required, but because of the two methods in interface A,//So even if the method body of the two methods is empty in the implementation process, the two methods do not work. 
  public void Method2 () {} public void Method3 () {} public void Method4 () {System.out.println ("Class D implementation Interface I, method 4"); 
  public void Method5 () {System.out.println ("Class D implements interface I, method 5"); 
    The public class client{public static void Main (string[] args) {A A = new A (); 
    A.depend1 (New B ()); 
    A.depend2 (New B ()); 
     
    A.depend3 (New B ()); 
    c C = new C (); 
    C.depend1 (New D ()); 
    C.depend2 (New D ()); 
  C.depend3 (New D ()); 
 } 
}

As you can see, if the interface is too bloated, as long as the methods that appear in the interface are useless to the classes that depend on it, it is clearly not a good design to implement these methods in the implementation class. If you modify this design to conform to the interface isolation principle, you must split the interface I. Here we split the original interface I into three interfaces, and split the design as shown in the following illustration:

Routinely post code for friends who are unfamiliar with the class diagram:

Interface I1 {public void method1 (); 
  } interface I2 {public void method2 (); 
public void method3 (); 
  } interface I3 {public void method4 (); 
public void Method5 (); 
  Class a{public void Depend1 (I1 i) {i.method1 (); 
  public void Depend2 (I2 i) {i.method2 (); 
  public void Depend3 (I2 i) {i.method3 (); 
  Class B implements I1, i2{public void method1 () {System.out.println ("Class B Implements interface I1 Method 1"); 
  public void Method2 () {System.out.println ("Class B Implementation Interface I2 Method 2"); 
  public void Method3 () {System.out.println ("Class B Implementation Interface I2 Method 3"); 
  Class c{public void Depend1 (I1 i) {i.method1 (); 
  public void Depend2 (I3 i) {i.method4 (); 
  public void Depend3 (I3 i) {i.method5 (); 
  Class D implements I1, i3{public void method1 () {System.out.println ("Class D Implements interface I1 Method 1"); 
  public void Method4 () {System.out.println ("Class D Implements interface I3 Method 4"); public void Method5 () {SYSTEM.OUT.PRINTLN ("Class D Implementation Interface I3 method 5"); 
 } 
}

The meaning of the

        interface isolation principle is to establish a single interface, not to create a large bloated interface, to refine the interface as much as possible, and to minimize the number of methods in the interface. That is, instead of trying to create a huge interface for all classes that depend on it, we need to create a dedicated interface for each class. In this example, the interface isolation principle is used to change a large interface to 3 dedicated interfaces. In programming, relying on several dedicated interfaces is more flexible than relying on a single integrated interface. Interfaces are design-time "contracts", which can be used to prevent the proliferation of external changes and improve the flexibility and maintainability of the system by defining multiple interfaces.
         Speaking here, many people feel that the principle of interface segregation is similar to the principle of single responsibility, but it is not. First, the principle of single responsibility focuses on responsibility, while the interface segregation principle pays attention to the isolation of interface dependencies. Second, the single responsibility principle is mainly the constraint class, secondly is the interface and the method, it is to implement and the detail in the program, but the interface isolation principle mainly constrains the interface interface, mainly aims at the abstraction, according to the program whole frame construction.
         when using the interface isolation principle to constrain an interface, note the following:
interface as small as possible, but with a limit. Refinement of the interface can improve the flexibility of programming is not a fact of earning, but if too small, it will cause too many interfaces, so that the design is complicated. So be sure to be modest. The
customizes the service for a class that is dependent on the interface, exposing only the methods it needs for the calling class, and the unwanted methods are hidden. Only by focusing on providing custom services to a module can a minimal dependency be established.
increase cohesion and reduce external interaction. Make the interface do the most things in the least possible way. The
uses the interface isolation principle, must moderate, the interface design is too big or too small is not good. When designing an interface, it takes more time to think and plan in order to practice this principle accurately.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.