当前位置:网站首页>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 .

The difference between composition and inheritance
- stay
Inheritance structurein , 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 ;) CombineBy 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 , stayCompile timeThen 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 inThe run-timedetermine .
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
边栏推荐
- 2022.04.23 (lc_763_divided into letter interval)
- Openlayers 5.0 loading ArcGIS Server slice service
- Installation, use and problem summary of binlog2sql tool
- 12个例子夯实promise基础
- Transaction processing of SQL Server database
- Accessing private members using templates
- c1000k TCP 连接上限测试
- SSDB基础2
- Introduction to micro build low code zero Foundation (lesson 3)
- An 8266 crash
猜你喜欢

Raspberry pie 18b20 temperature

MySQL restores or rolls back data through binlog

Class loading process of JVM

Use of fluent custom fonts and pictures

网络协议之:sctp流控制传输协议

2022.04.23 (lc_763_divided into letter interval)

为何PostgreSQL即将超越SQL Server?

简化路径(力扣71)

First experience of using fluent canvas
![[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]](/img/47/521bd7f144b0d6a5759d10067c9bea.png)
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
随机推荐
Getting started with vcpkg
Screen right-click menu in souI
Xlslib use
深度学习环境搭建步骤—gpu
Minesweeping II of souI instance
Introduction to micro build low code zero Foundation (lesson 3)
Installation, use and problem summary of binlog2sql tool
static类变量快速入门
arcgis js api dojoConfig配置
MySQL Téléchargement et installation de la version Linux
Using Visual Studio code to develop Arduino
std::stoi stol stoul stoll stof stod
JS controls the file type and size when uploading files
Transaction processing of SQL Server database
【C语言进阶11——字符和字符串函数及其模拟实现(2))】
Client interns of a large factory share their experience face to face
Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
SSDB foundation 3
redis优化系列(三)解决主从配置后的常见问题
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)