Java and mode Reading Note 5-interface isolation principle (ISP) and synthesis/aggregation Reuse Principle (CARP)

Source: Internet
Author: User

Interface isolation principle (ISP): it is better to use multiple special interfaces than to use a single total interface.

The dependence of a class on another class should be based on the smallest interface.

An interface represents a role, and different roles should not be assigned to an interface. Unrelated interfaces are merged to form a bloated large interface, which is a pollution to roles and interfaces.

 

Synthesis/aggregation Reuse Principle (CARP): Also known as the synthesis Reuse Principle (CRP), is to use some existing objects in a new object to make it a part of the new object; new objects reuse existing functions by assigning them. In short, we should try to use synthesis/aggregation instead of inheritance.

Aggregation: used to indicate the "owned" relationship or the relationship between the whole and the part.

Synthesis: used to indicate a much stronger "ownership" relationship. In a synthesis, the partial and overall life cycles are the same. A new object to be merged has full right to control its components, including their creation and elimination.

The multiple features of a synthesis cannot pass through one. The component objects in a synthesis relationship cannot be shared with another synthesis relationship. A component object can belong to only one compositing relationship.

Basic Types of multiplexing:

Merging/aggregation multiplexing:

Advantages:

* The only way for a new object to access a component object is through the component object interface.

* This reuse method is black box reuse, because the internal details of component objects are invisible to new objects.

* This type of reuse supports packaging.

* This reuse requires less dependencies.

* Each new class can focus on a task.

* This type of reuse can be performed dynamically during running time. New objects can dynamically reference objects of the same type as component objects.

Disadvantages:

* Many objects need to be managed in the system.

Reuse through inheritance:

Synthesis/aggregation can be applied to almost any environment as a means of reuse, but inheritance can only be used in a limited environment. Although inheritance is a very important method of reuse, synthesis/aggregation should be considered first, rather than inheritance.

Advantages:

* The new implementation is easier, because most of the functions of the superclass can automatically enter the subclass through the inherited relationship.

* It is easier to modify and extend the inherited implementations.

Disadvantages:

* Inheritance reuse destroys the packaging because inheritance exposes the implementation details of the superclass to the subclass. Because the internal details of superclasses are often transparent to sub-classes, this reuse is transparent, also known as "white box" reuse.

* If the superclass changes, the implementation of the subclass must also change.

* The implementation inherited from the superclass is static and cannot be changed during the running time, without sufficient flexibility.

 

When re-purchasing a design that violates the Li's replacement principle, there are two methods: one is to add an abstract superclass, and the other is to change inheritance to a synthetic/aggregate relationship.

"Is-A" indicates that one class Is another class. "Has-A" indicates that one class Is A role of another class, rather than A special class. Inheritance should be considered for "Is-A", while "Has-A" uses synthesis/aggregation.

Related Article

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.