Interface isolation (ISP) in Java)

Source: Internet
Author: User

The dependency of one class on another class should be expressed as an interface with as little dependencies as possible.

This principle is used to handle the defects of fat interfaces and avoid the interface from taking too many responsibilities. For example, methods in an interface can be divided into several groups for different customers.ProgramService, indicating that this interface is too fat. Of course, there are also some classes that do not need cohesion interfaces, but these classes should not be directly seen by the client program as separate classes, but should be accessed through abstract base classes or interfaces.

Interface pollution
The so-called interface pollution is to add unnecessary responsibilities to the interface. Add a new method to the interface to benefit the implementation class and reduce the number of classes. In this way, the interface is constantly polluted and fatter. In fact, the number of classes is not a problem at all. Interface pollution will cause maintenance and reuse problems. The most common problem is that we are forced to implement and maintain unnecessary methods to reuse contaminated interfaces.

Separating a client program is a splitting interface. If the customer program is separated, the corresponding interfaces should also be separated because the customer program has a reaction to the interfaces they use. Generally, when the interface changes, we need to consider how all the customer programs that use the interface change to adapt to the interface changes. What if the client program changes? In this case, you also need to consider whether the interface needs to change. This is the reaction force. Sometimes the changes in business rules are not so direct, but caused by changes in the customer program. In this case, we need to change the interface to meet the needs of the customer program.

There are two methods to separate interfaces: Delegation and multi-inheritance. The former delegates requests to the Implementation classes of other interfaces to fulfill the required responsibilities. The latter implements multiple interfaces to fulfill the required responsibilities. The two methods have their own advantages and disadvantages. Generally, we should consider the latter scheme first. If type conversion is involved, select the former scheme.

The fat interface will lead to unnecessary coupling between the customer program, and the whole body will be moved. The decomposition of the fat interface enables the customer program to only rely on the methods it needs. In terms of design, it is easy to maintain and highly reusable.

 

Let's take a look at the following article.Article

Summary clients shocould not be forced to depend upon interfaces that they do not use.
Users cannot be forced to rely on interfaces they do not use. In other words, it is better to use multiple dedicated interfaces than to use a single total interface.
It contains two meanings:
-Interface design principle: the interface design should follow the minimum interface principle. Do not insert unused methods into the same interface.
If an interface method is not used, it indicates that the interface is too fat and should be divided into several specific functions.
-Interface dependency (inheritance) Principle: if an interface a depends on (inherits) Another interface B, interface A is equivalent to the method inherited from interface B, interface A inherited from interface B should also follow the above principle: it should not contain methods not used by users.
Otherwise, it indicates that interface A is contaminated by B and its relationship should be re-designed.

If users are forced to rely on interfaces they do not use, they also have to change when the interfaces change. In other words, a user is dependent on interfaces that are not used but used by other users. When other users modify this interface, all users dependent on this interface will be affected. This clearly violates the open and closed principle and is not what we expect.

The following is an example of how to design the relationship between interfaces or classes so that they do not violate the ISP principle.
Assume that there is a door with the lock and unlock functions. In addition, you can install an alarm on the door to enable the alarm function. Users can select either a common door or a door with the alarm function.

There are several design methods:

ISP violation examples:

Method 1:
Define all methods in the door interface. Figure:

But in this way, the commondoor that relies on the door interface has to implement the unused alarm () method. Violation of ISP principles.

Method 2:
The alarm interface defines the alarm method, the door interface defines the lock and Unlock methods, and the door interface inherits the alarm interface.

Like method 1, commondoor that relies on the door interface has to implement the unused alarm () method. Violation of ISP principles.

Example of ISP principles:

Method 3: implement multiple inheritance

Define the Alarm Method on the alarm interface and the lock and Unlock methods on the door interface. There is no inheritance relationship between interfaces. Commondoor implements the door interface,
Alarmdoor has two implementation solutions:
(1) implements both the door and alarm interfaces.
2) inherits commondoor and implements the alarm interface. This solution is an implementation of the inherited adapter design mode.
2nd) more practical solutions.
This design follows the ISP design principles.

Method 4: implemented through commissioning

This method is actually the implementation of the adapter design pattern of the proposed approach.
In this method, alarmdoor implements the alarm interface and grants the lock and unlock functions to the commondoor object.
This design follows the ISP design principles.

Summary interface segregation principle (ISP) establishes a judgment benchmark for the abstract granularity of the interface from the use of the interface: when designing an interface for the system, use multiple special interfaces to replace a single fat Interface

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.