Class inheritance and object combination (extract design mode)

Source: Internet
Author: User

Class inheritance and object combination are the two most common technologies in OOB.
1. class inheritance
Also known as white box reuse, white box reuse.
Advantages:
Class inheritance can easily change the implementation of reuse. When a subclass redefinition of some rather than all operations, it can also affect its inherited operations.
Disadvantages:
It is defined at the Compilation Time, and the implementation of inheriting from the parent class cannot be changed at the runtime.
Subclass implementation is closely dependent on the parent class. Any changes made to the implementation of the parent class will affect the subclass. This dependency limits flexibility and reusability.
One solution is to inherit only the abstract base class. abstract classes generally provide fewer implementations.
2. Object Composition)
Also known as black box reuse, black box reuse.
Object combinations are dynamically defined at runtime by obtaining references to other objects.
Advantage: Because objects can only be accessed through interfaces, encapsulation is not broken. As long as the types are consistent, one object can be used to replace another object at runtime.
Because the implementation of objects is written based on interfaces, there is little dependency between implementations.
In addition, the combination of priorities helps to keep the class encapsulation and concentrate on a single task. In this way, the hierarchy of classes and classes remains relatively small and will not grow into an uncontrollable giant.
In addition, the Design Based on Object combination has more objects instead of fewer classes, and the system behavior will depend on the relationship between objects rather than being defined in a class.
So,
"Object combination is preferred, rather than class inheritance"

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.