Synthesis/aggregation Reuse Principle (CARP)

Source: Internet
Author: User
The composite/aggregate Reuse Principle or CARP principle is to use some existing objects in a new object to make it part of the new object, new objects reuse existing functions by assigning them. These two types are associated with each other. Aggregation indicates the relationship between the whole and the part, and the part can be separated from the whole as an independent individual. combination is a stronger aggregation, and the part is composed of the whole, however, some cannot exist independently, and some lifecycles cannot exceed the overall lifecycle. Aggregation is like a computer and a mouse, and a combination is like a person and a heart.
Combination, aggregation, and inheritance are two basic ways to achieve reuse. Inheritance reuse gets new functions by extending the implementation of an existing object. The base class is responsible for common attributes and methods, subclass extends the base class by adding new properties and methods, and combines/aggregates and reuse existing objects into the new object to make them part of the new object, new objects can call existing objects.
The two methods have their own advantages and disadvantages:
Inheritance reuse, the advantage is that new implementations or modifications are easier, and the super class function inherits to the subclass. However, inheritance reuse destroys encapsulation, and super class changes lead to subclass changes. The implementation of super class inheritance is static, and changes occur when it cannot be run again, resulting in insufficient flexibility.
Combination/aggregation multiplexing. The disadvantage is that many objects need to be managed and interfaces are strictly defined to implement different objects as composite blocks. The advantage is that encapsulation is supported, there are few dependencies. The new class can focus on a task, and the internal details are not visible to the new object. The new object can only read the component object through the interface, new objects can be dynamically used in component objects.
Experience tells us that we should "try to use combination/aggregation, and try not to use inheritance ". That is, object combination/aggregation is preferred, rather than inheritance.
There are the following rules for using inheritance relationships:
① Subclass is a special category of superclasses, rather than a role of superclasses.
② There will never be a situation where you need to replace the subclass with the subclass of another class. If you are not sure whether it will become another subclass in the future, do not use inheritance.
③ The subclass has the responsibility to expand the superclass, rather than replacing or canceling the superclass. If a subclass needs to replace a large number of superclasses, this class should not be a subclass of this superclass.

Synthesis/aggregation Reuse Principle (CARP)

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.