当前位置:网站首页>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
边栏推荐
- [play with lighthouse] Tencent cloud lightweight server builds a full platform video analysis video download website
- arcgis js api dojoConfig配置
- Circuit on-line simulation
- Using bafayun to control the computer
- [report] Microsoft: application of deep learning methods in speech enhancement
- Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
- Some speculation about the decline of adults' language learning ability
- Modify the font size of hint in editext
- Core concepts of rest
- SSDB foundation 1
猜你喜欢

2021-2022-2 ACM集训队每周程序设计竞赛(8)题解

2022.04.23(LC_714_买卖股票的最佳时机含手续费)

MySQL Téléchargement et installation de la version Linux

On the forced conversion of C language pointer

mysql_linux版本的下载及安装详解

SQL常用的命令

Raspberry pie uses root operation, and the graphical interface uses its own file manager

mysql_ Download and installation of Linux version

12 examples to consolidate promise Foundation

MySQL学习第五弹——事务及其操作特性详解
随机推荐
什么是消息队列
Network protocol: SCTP flow control transmission protocol
JS controls the file type and size when uploading files
深度学习环境搭建步骤—gpu
I just want to leave a note for myself
2022.04.23 (lc_763_divided into letter interval)
MySQL restores or rolls back data through binlog
C: generic reflection
Codeforces Round #784 (Div. 4)
腾讯云GPU最佳实践-使用jupyter pycharm远程开发训练
SSDB基础
Problems caused by flutter initialroute and home
Codeforces Round #784 (Div. 4)
The difference between ordinary inner class and static inner class
Introduction to micro build low code zero Foundation (lesson 3)
All table queries and comment description queries of SQL Server
Wechat applet part of the mobile phone Preview PDF did not respond
ArcGIS JS API dojoconfig configuration
One stop service platform for high-level talents and development of comprehensive service platform system for talents
Disable Ctrl + Alt + Del