6 design principles of the interface isolation principle (Interface segregation principle). __ Design mode

Source: Internet
Author: User

The interface is divided into two types.

Instance interface (Object Interface), declares a class in Java, and then produces an instance with the New keyword, which is a description of a type of thing, which is an interface. For example, you define the class of person, and then use person Zhangsan = the new person () to produce an instance, the standard that the instance should follow is the class of person, the person class is the Zhangsan interface, and the class in Java is also an interface. Class Interface, the interface defined by the Interface keyword that is often used in Java.
Definition of the interface isolation principle:

The client should not rely on interfaces that it does not need. Dependencies between classes should be based on the smallest interface. We can generalize these two definitions into one sentence: Establish a single interface and do not create a bloated and bulky interface. More popular point: the interface as much as possible, while the interface in the method as little as possible. It is not the same as the principle of single duty to see that there may be doubts. Wrong, the interface isolation principle and the perspective of a single responsibility is not the same, the single responsibility requires a single class and interface responsibilities, focus on the responsibility, which is the division of Business logic, and the interface isolation principle requires that the interface method as little as possible.

Two types of beauty definitions

Public interface Igoodbodygirl {

To have a good-looking face.

public void goodlooking ();

To have a good body

public void Nicefigure ();

}

Public interface Igreattemperamentgirl {

To have temperament

public void greattemperament ();

}

The most standard beauty

public class Pettygirl implements Igoodbodygirl, Igreattemperamentgirl {

private String name;

All the beauties have names.

Public Pettygirl (String _name) {

THIS.name = _name;

}

Pretty face.

public void goodlooking () {

System.out.println (this.name + "---face is very beautiful. ");

}

Better temperament.

public void Greattemperament () {

System.out.println (this.name + "---temperament is very good. ");

}

He's a better figure.

public void Nicefigure () {

System.out.println (THIS.name + "---the body is very good. ");

}

}


The interface isolation principle is a specification constraint on an interface that contains the following 4 meanings:

interface to be as small as possible. When you split an interface based on the interface isolation principle, you must first meet the single responsibility principle. interface to high cohesion specific to the principle of interface isolation, the requirements in the interface to publish the public method as little as possible, the interface is an external commitment, the less commitment to the development of the system more favorable, the less risk of change, but also conducive to reducing costs. Custom Service Customization service is to provide an individual with an excellent service. We also need to consider the use of custom services between systems or between modules in our system design. There must be a requirement for a custom service: to provide only the methods the visitor needs. There is a limit to the interface design

Best Practices:

The interface isolation principle is the definition of an interface, as well as the definition of a class, in which interfaces and classes are assembled with atomic interfaces or atomic classes as much as possible. In practice, the following rules can be used to measure:

An interface serves only one child module or business logic; Through the business logic compression interface in the public method, the interface is often reviewed, as far as possible to reach the interface "covered with bones and muscles", rather than "fat doodle" a lot of methods, has been contaminated the interface, as far as possible to modify, if the risk of change is greater, The adapter mode is used to transform the process; Understand the environment, refuse to follow blindly. Each project or product has specific environmental factors, do not look at the master is doing so you copy. The standards for interface splitting are different for different environments. Deep understanding of business logic.
How to practice the interface isolation principle exactly. Practice, experience and understanding.

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.