) Interface segregation principle (ISP)-oo interface separation principle

Source: Internet
Author: User

Preface: Robert C. martin summarizes the principles that should be followed in Object-Oriented Design (OOD). These principles are called "prinles les of OOD ", for more information about principles of OOD, see object menter.

This article introduces the interface separation principle in "Principles of OOD": interface segregation principle (ISP ).

You can view the original interface segregation principle (ISP) from here.

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, multiple special interfaces are used to replace a single fat interface.

Reference the interface segregation principle (ISP) by Robert C. Martin .-

Article Source: http://www.diybl.com/course/3_program/rg/2008629/129246.html

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.