Introduction:
In our careers, we often hear colleagues from the same industry say: “We must target interface programming, and Not for implementation programming”; “Prefer combination over inheritance”. So today I will talk about object composition and inheritance.
- What is inheritance and combination
- The advantages and disadvantages of inheritance
- The advantages and disadvantages of combination
< h3 id="What is inheritance and composition">What is inheritance and composition
Class inheritance or subclassing allows us to use other classes to define the implementation of a class (using superClass to Define the implementation of subClass). Subclassing is often called white-box reuse, because the internal description and details of superClass are usually visible to subclasses.
Object composition requires that the combined object has a good interface and is defined at runtime through references obtained from other objects. So you can combine objects into other objects to build more complex functions. Since the internal details of objects are not visible to other objects, they appear to be “black boxes”. This type of reuse is called black-box reuse.
This is what we often call white box multiplexing and black box multiplexing.
The advantages and disadvantages of inheritance
So what are the advantages and disadvantages of using class inheritance or subclassing? :
Advantages:
1, class inheritance is simple and crude, intuitive, and the relationship is statically defined at compile time.
2. The reused implementation is easy to modify, and the sub can cover the super implementation.
Disadvantages: 1. The implementation inherited from super cannot be changed at runtime (not necessarily a disadvantage)
2, part of the implementation of sub is usually defined in super.
3, sub directly faces the implementation details of super, thus destroying the Fengda column Talking about object composition and inheritance installation.
4, any change in super implementation will force subclasses to also change, because their implementations are linked together.
5. If the inherited implementation is outdated or not applicable in the new problem scenario, it is necessary to rewrite super or the inherited implementation.
Due to the dependency of implementation in class inheritance, it may be problematic to reuse subclasses. One solution is to only inherit from the protocol or abstract base class (subtyping), because they are only rarely implemented, while the protocol is not implemented.
combination advantages and disadvantages
Object composition allows us to use multiple objects at the same time, and each object It is assumed that the interfaces of other objects are functioning normally. Therefore, in order to operate normally in the system, their interfaces need to be carefully designed. Let me talk about his advantages and disadvantages.
Advantages: 1. It will not break the encapsulation, because the object is accessed only through the interface;
2. Reduce the dependency of the implementation, because the reality is defined through the interface ;
3, you can replace any object with other objects of the same type at runtime;
4, you can keep the encapsulation of the class to focus on a single task;
5, the class and its hierarchical structure can be maintained Concise, not excessively inflated and unmanageable;
Disadvantages: 1, involving many objects;
2, the behavior of the system will depend on the relationship between different objects, rather than being defined in a single class;
3 , Ready-made components are always not enough, which leads us to constantly define new objects.
Conclusion:
Despite the above shortcomings, object composition still has many benefits to system design. We can overcome these shortcomings by using class inheritance in some parts, making it easier to use existing components to create new components. Of course, when using class inheritance, use class inheritance.