ISP interface isolation principle

Source: Internet
Author: User
I. Definition

The customer program should not be forced to rely on unused methods.

2. Interface pollution

Interface contamination is common in static language such as C # And C ++. An interface is contaminated by methods that it does not need. In an interface, assume that a method is only used to benefit one of its sub-classes. If this continues, this method will be added to the base class every time the subclass requires a new method. This will further pollute the interface of the base class and make it "fat ".

In addition, each time a method is added to the base class, this method must be implemented in the derived class (or a default implementation is defined ). In fact, there is a specific practice that allows a derived class to not implement these methods. The practice is to combine these interfaces into a base class, this base class also provides degradation implementation of methods in interfaces. However, this implementation violates the LSP and brings about maintenance and reuse problems.

3. Separate customers or separate Interfaces

If customer programs are forced to rely on methods they do not use, these customer programs face changes due to these unused method changes. This inadvertently leads to coupling between all customer programs. In other words, if a client program depends on a class that contains methods that it does not use, but other client programs actually need to use this method, when other customers require this class to change, this will affect the customer program. We want to avoid this coupling as much as possible, so we want to separate the interface.

Iv. Class interface and object interface

1. Use the "delegate" isolation interface (adapter Mode)

2. Use the multi-inheritance isolation Interface

V. Conclusion

Class will lead to abnormal and harmful coupling between their customer programs. When a program customer requests a change to the fat class, it will affect all other customer programs. Therefore, the customer program should only rely on the methods they actually call. This goal can be achieved by decomposing the fat class interface into multiple client-specific interfaces. Each client-specific interface only declares the functions called by specific customers or customer groups. Then, the fat class can inherit all the interfaces specific to the customer program and implement them. This removes the dependency between the customer program and the methods they do not call, and makes the customer program independent of each other.

ISP 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.