当前位置:网站首页>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 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 , stayCompile 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 inThe 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
边栏推荐
- Introduction to micro build low code zero Foundation (lesson 3)
- PostgreSQL
- 坐标转换WGS-84 转 GCJ-02 和 GCJ-02转WGS-84
- Using 8266 as serial port debugging tool
- mysql通过binlog恢复或回滚数据
- Strange passion
- 机器学习目录
- On the forced conversion of C language pointer
- Coordinate conversion WGS-84 to gcj-02 and gcj-02 to WGS-84
- Raspberry pie 18b20 temperature
猜你喜欢
mysql_linux版本的下载及安装详解
Why is PostgreSQL about to surpass SQL Server?
【C语言进阶11——字符和字符串函数及其模拟实现(2))】
Openharmony open source developer growth plan, looking for new open source forces that change the world!
mysql_ Download and installation of Linux version
Matlab 2019 installation of deep learning toolbox model for googlenet network
优先使用组合而不使用继承
The platinum library cannot search the debug process records of some projection devices
网络协议之:sctp流控制传输协议
Raspberry pie 18b20 temperature
随机推荐
Strange problems in FrameLayout view hierarchy
腾讯云GPU最佳实践-使用jupyter pycharm远程开发训练
The difference between ordinary inner class and static inner class
Using bafayun to control the computer
Quick start to static class variables
Sogou cell thesaurus analysis (only extract words and word frequency)
MySQL学习第五弹——事务及其操作特性详解
FTP、ssh远程访问及控制
How about CICC wealth? Is it safe to open an account up there
Some ideas about time-consuming needs assessment
Switching power supply design sharing and power supply design skills diagram
C: generic reflection
Common SQL commands
Simplified path (force buckle 71)
Screen right-click menu in souI
SQL of contention for system time plus time in ocrale database
FTP, SSH Remote Access and control
UML类图几种关系的总结
Openlayers 5.0 reload the map when the map container size changes
arcMap 发布切片服务