Six design principles of Zen design pattern-Interface Isolation principle

Source: Internet
Author: User
Tags what interface

Interface Isolation principle
One: What is an interface?
Instance interface (Object Interface)
---->person zhangsan=new person () produces an instance, the standard that the instance is to follow is the person class, the person class is the interface of the Zhangsan
Class interface (Classes Interface)
The interface defined by the interface keyword that is often used---->java.


Two: What is isolation? It has two definitions:
----> The definition of things are generally difficult to understand, obscure and difficult to understand is normal. Let's dissect these two definitions.
----> The first definition: The client should not rely on the interface it does not need ", what does it depend on? Depending on the interface it needs, what interface the client needs to provide, eliminate the unwanted interface, it needs to refine the interface to ensure its purity.
----> Second definition: The dependency between classes should be based on the smallest interface, it requires the smallest interface, also requires the interface refinement, the interface is pure, and the first definition is the same, just a thing of two different descriptions.
----> Summary: Create a single interface, do not build a bloated and huge interface. A little more popular: interface as much as possible, and the interface of the method as little as possible
----> Confusion: See here you may have to wonder, this is not the same as the single principle of responsibility? The interface isolation principle is different from the view of the single duty, and the single duty requires a single function of class and interface, and the responsibility is the division of Business logic. The interface isolation principle requires that the interface method be as few as possible. For example, the responsibility of an interface may contain 10 methods, these 10 methods are placed in an interface, and provided to a plurality of modules access, each module according to the required permissions to access, outside the system through the document constraints "do not use access to", according to the principle of single responsibility is allowed, according to the principle of interface isolation is not allowed , as it requires "to use as many specialized ports as possible". What does a dedicated interface mean? The one that is provided to each module should be a single interface, provided to several modules should have several interfaces, rather than build a huge bloated interface, accommodating all client access.

Example:
Beauty is divided into face temperament beauty and body beauty
The interface of a face temperament
An interface for a beautiful figure.
An abstract class has a dependency of two interfaces


The interface of a face temperament
Package com.yeepay.sxf.sjms1;
/**
* Facial Temperament Beauty Interface
* @author SXF
*
*/
Public interface Igoodbodygirl {
/**
* A beautiful facial appearance
*/
public void goodlooking ();
/**
* Have temperament
*/
public void greattemperament ();

}


An interface for a beautiful figure.
Package com.yeepay.sxf.sjms1;
/**
* Body Beauty
* @author SXF
*
*/
public interface igreattemperamentgirl{

/**
* Good body
*/
public void Nicefigure ();
}



An abstract class has a dependency of two interfaces
Package com.yeepay.sxf.sjms1;
/**
* The abstract class of the Star Scout
* @author SXF
*
*/
Public abstract class AbstractSearcher2 {
/**
* Face Temperament Beauty
*/
protected Igoodbodygirl Igoodbodygirl;
/**
* Face Temperament Body Beauty
*/
protected Igreattemperamentgirl Igreattemperamentgirl;
/**
* Face Temperament Beauty
* @param pettygirl
*/
Public AbstractSearcher2 (Igoodbodygirl igoodbodygirl) {
This.igoodbodygirl=igoodbodygirl;
}
/**
* Face Temperament Body Beauty
* @param igreattemperamentgirl
*/
Public AbstractSearcher2 (Igreattemperamentgirl igreattemperamentgirl) {
This.igreattemperamentgirl=igreattemperamentgirl;
}
/**
* Search for Beauty
*/
public abstract void Show ();
}
Three: How to ensure the purity of the interface
The interface should be as small as possible
---> This is the core definition of the interface isolation principle, there is no bloated interface (Fat Interface), but "small" is limited, first of all, cannot violate the principle of single responsibility
---> Split interfaces based on interface isolation principles, you must first meet the principle of single responsibility

interface to high cohesion
---> What is high cohesion? High cohesion is to improve the interface, class, module processing power, reduce external interaction. For example, you tell your subordinates to "steal a XXX file from Obama's office," and then hear subordinates answer you in a firm tone: "Yes, guaranteed to complete the mission!" One months later, your subordinates really put the xxx file on your desk, the behavior that does not say any conditions, the immediate completion of the task is high cohesion performance. Specific to the interface isolation principle is to require in the interface as little as possible to publish public methods, interface is the commitment of the external, the less commitment to the development of the system is more favorable, the risk of change will be less, but also conducive to reduce costs.

Customized Services
---> There is bound to be a coupling between the modules within a system or system, there is a need to interface with each other (not necessarily the interface defined in Java, or a class or a simple data exchange), and we design a service that needs to be customized for each visitor (that is, the client). What is a custom service? Custom service is to provide an individual with an excellent service. We also need to consider customizing the interface between the system or the module when we do the system design. There must be a requirement to use custom services: Only the methods that the visitor needs are provided

There is a limit to the design of the interface
The smaller the granularity of the design of the---> interface, the more flexible the system, which is an indisputable fact. However, the flexibility also brings the complexity of the structure, the development difficulty increases, maintainability is reduced, this is not a project or product expected to see, so the interface design must pay attention to moderate


IV: Best Practices
The interface isolation principle is the definition of an interface, as well as the definition of a class, where interfaces and classes are assembled using atomic interfaces or atomic classes whenever possible. However, how this atom is divided is a major problem in the design pattern, which can be measured in practice according to the following rules:
An interface serves only one submodule or business logic;
Through the business logic compression interface in the public method, the interface often to review, as far as possible to get the interface to "flesh", rather than "fat doodle" a lot of methods;
has been contaminated with the interface, as far as possible to modify, if the risk of a large change, the use of adapter mode for conversion processing;
Understand the environment and refuse to follow blindly. Each project or product has specific environmental factors, don't look to the master to do so you copy. Do not, the environment is different, interface split standards are different. In-depth understanding of business logic, the best interface design comes from your hands!

Six design principles of Zen design pattern-Interface Isolation principle

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.