当前位置:网站首页>Use object composition in preference to class inheritance
Use object composition in preference to class inheritance
2022-07-05 03:54:00 【Bouncing bit】
《 Extreme programming (xp) 》(Extreme programming) One of the guiding principles of is “ As long as you can use , Just do the simplest ”. A design that seems to need inheritance can often be dramatically simplified by using composition instead , So as to make it more flexible . therefore , When considering a design , Ask yourself :“ Is it easier to use combination ? Do you really need to inherit here ? What benefits can it bring ?”
Comparison of inheritance and composition :
The two most commonly used technologies of function reuse in object-oriented systems are class inheritance and object composition (object composition). As we have explained , Class inheritance allows you to define the implementation of a class according to the implementation of other classes . This reuse by generating subclasses is usually called white box reuse (white-box reuse). The term “ White box ” It's relative visibility : In the way of inheritance , The inner details of the parent class are visible to the child class .
Object composition is an alternative to class inheritance . New and more complex functions can be achieved by assembling or combining objects . Object composition requires that the object to be combined has a well-defined interface . This style of reuse is called black box reuse (black-box reuse), Because the internal details of an object are invisible . The object is only “ black box ” In the form of .
Inheritance and composition have their own advantages and disadvantages . Class inheritance is defined statically at compile time , And it can be used directly , Because programming languages directly support class inheritance . Class inheritance can easily change the reused implementation . When a subclass redefines some but not all operations , It can also affect the operations it inherits , As long as the redefined operation is called in these operations .
But class inheritance also has some shortcomings . First , Because inheritance is defined at compile time , So you can't change the implementation inherited from the parent class at run time . To make matters worse , A parent class usually defines at least some specific representations of its subclasses . Because inheritance reveals the implementation details of its parent class to its child class , So inheritance is often considered “ Breaking the encapsulation ” . The implementation in a subclass has such a close dependency on its parent , So that any change in the implementation of the parent class will inevitably lead to changes in the subclass . When you need to reuse subclasses , Implementation dependencies can cause problems . If the inherited implementation is not suitable for solving new problems , Then the parent class must be overridden or replaced by another more suitable class . This dependency limits flexibility and ultimately reusability . An available solution is to inherit only abstract classes , Because abstract classes usually provide fewer implementations .
Object composition is defined dynamically at run time by obtaining references to other objects . Composition requires objects to abide by each other's interface conventions , Further, it requires more careful definition of interfaces , These interfaces do not prevent you from using one object with other objects . This will also produce good results : Because objects can only be accessed through interfaces , So we don't break encapsulation ; As long as the types are the same , Runtime can also replace one object with another ; Further more , Because the implementation of the object is based on the interface , So there are fewer dependencies on the implementation .
Object composition has another effect on system design , That is, using object composition first helps you keep every class encapsulated , And be focused on a single task . In this way, the class and class inheritance level will remain small , And is unlikely to grow into an uncontrollable behemoth . On the other hand , Design based on object composition will have more objects ( There are fewer classes ), And the behavior of the system will depend on the relationship between objects rather than being defined in a class .
This leads to our second principle of object-oriented design : Use object combination first , Instead of class inheritance .
Link to the original text :https://www.cnblogs.com/nexiyi/archive/2013/06/16/3138568.html
边栏推荐
- 【软件逆向-基础知识】分析方法、汇编指令体系结构
- [untitled]
- Difference between MotionEvent. getRawX and MotionEvent. getX
- 反絮凝剂-氨碘肽滴眼液
- MySQL winter vacation self-study 2022 11 (10)
- NPM introduction link symbolic link
- v-if VS v-show 2.0
- Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
- 英语必备词汇3400
- Web components series (VII) -- life cycle of custom components
猜你喜欢
[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)
NEW:Devart dotConnect ADO. NET
Clickhouse同步mysql(基于物化引擎)
Timing manager based on C #
Use of vscode software
线上故障突突突?如何紧急诊断、排查与恢复
How to define a unified response object gracefully
花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书
特殊版:SpreadJS v15.1 VS SpreadJS v15.0
为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
随机推荐
Use of vscode software
Nmap user manual learning records
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
CTF stegano practice stegano 9
Enterprise level: spire Office for . NET:Platinum|7.7. x
MindFusion.Virtual Keyboard for WPF
Kubernetes -- cluster expansion principle
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 1)
Thread Basics
@The problem of cross database query invalidation caused by transactional annotation
Plasticscm enterprise crack
Delphi read / write JSON format
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
How is the entered query SQL statement executed?
Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
speed or tempo in classical music
Basic knowledge of tuples
我就一写代码的,王总整天和我谈格局...
测试开发是什么?为什么现在那么多公司都要招聘测试开发?
speed or tempo in classical music