当前位置:网站首页>Prefer composition to inheritance

Prefer composition to inheritance

2022-04-23 19:16:00 Eighteen movements of Tathagata divine palm

Java It's a object-oriented Language . encapsulation 、 Inherit 、 Polymorphism is the three characteristics of object-oriented . Whether studying or working, it may need to be reused , The first word I think of is : Inherit . But in design patterns , Find that composition is a good way to reuse , It applies to most situations where we need to reuse , So use composition first rather than inheritance .

Inherit

Inherit (Inheritance) It's a hierarchical model that connects classes and classes . It refers to a class ( be called Subclass 、 A subinterface ) Inherit another class ( Called the parent class 、 The parent interface ) The function of , And the ability to add its own new features , Inheritance is the most common relationship between classes or interfaces ; Inheritance is a kind of is-a Relationship .( Picture from the Internet , Invasion and deletion .)

Combine

Combine (Composition) It's a weak relationship , It embodies the whole and the part 、 The relationship you have , namely has-a The relationship between .
 Picture description here

The difference between composition and inheritance

  • stay Inheritance structure in , The inner details of the parent class are visible to the child class . So we can usually say that code reuse through inheritance is a kind of white box code reuse . If the implementation of the base class changes , Then the implementation of the derived class will change . This leads to the unpredictability of subclass behavior ;)
  • Combine By assembling existing objects ( Combine ) Generate new 、 More complex features . Because between objects , The internal details of each are invisible , So we also say that code reuse in this way is black box code reuse .( Because a type is usually defined in a combination , So I don't know which implementation class's method will be called at compile time )
  • Inherit , When you write code, you need to specify which class to inherit , therefore , stay Compile time Then the relationship is established .( The implementation inherited from the base class cannot be dynamically changed at run time , Therefore, the flexibility of application is reduced .)
  • Combine , Interface oriented programming can be used when writing code . therefore , The combinatorial relationship of classes is generally in The run-time determine .

Comparison of advantages and disadvantages

Group close Turn off system Following bearing Turn off system
advantage : Do not break the package , Loose coupling between whole class and local class , Relatively independent of each other shortcoming : Break the package , Subclasses are closely coupled with their parents , Subclasses depend on the implementation of their parents , Subclasses lack independence
advantage : It has good scalability shortcoming : Support extended , But often at the cost of increasing the complexity of the system structure
advantage : Support dynamic composition . At run time , The whole object can select different types of local objects shortcoming : Dynamic inheritance is not supported . At run time , A child cannot choose a different parent class
advantage : The whole class can pack the local class , Encapsulate the interface of the local class , Provide new interfaces shortcoming : A subclass cannot change the interface of a parent class
shortcoming : The whole class cannot automatically obtain the same interface as the local class advantage : Subclass can automatically inherit the interface of the parent class
shortcoming : When creating objects of the whole class , You need to create objects of all departments advantage : When you create an object of a subclass , There is no need to create an object of the parent class

summary

Why composition takes precedence over inheritance , Or use more combinations and less inheritance , Because composition is really more flexible than inheritance , It's also more helpful for code maintenance . Then whether inheritance is good for nothing , Not necessarily .

In object-oriented programming , One of the most likely ways to create and use code is : Encapsulate data and methods into a class , And use that kind of object . Sometimes , Need to pass “ synthesis ” Technology uses ready-made classes to construct new classes . Inheritance is the rarest practice . therefore , Although inheritance is learning OOP The process has received a lot of emphasis , But that doesn't mean it should be used everywhere possible . contrary , Be very careful when using it . Only when it is clear that inheritance is the most effective of all methods , Before you can consider it . To determine whether you should choose composition or inheritance , One of the easiest ways to do this is to consider whether you need to trace the modeling from the new class back to the basic class . If you have to go back , We need to inherit . But if you don't need to backtrack modeling , You should remind yourself to prevent the abuse of inheritance .

版权声明
本文为[Eighteen movements of Tathagata divine palm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231912079237.html