当前位置:网站首页>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
边栏推荐
- 程序员的视力怎么样? | 每日趣闻
- Containerization Foundation
- error Couldn‘t find a package. JSON file in "your path“
- [wp]bmzclub writeup of several questions
- New interesting test applet source code_ Test available
- v-if VS v-show 2.0
- How to use jedis of redis
- Necessary fonts for designers
- NEW:Devart dotConnect ADO. NET
- [groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)
猜你喜欢
Yuancosmic ecological panorama [2022 latest]
How to use jedis of redis
How to define a unified response object gracefully
我就一写代码的,王总整天和我谈格局...
Clickhouse物化视图
CTF stegano practice stegano 9
为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
Web components series (VII) -- life cycle of custom components
UI自動化測試從此告別手動下載瀏覽器驅動
随机推荐
Redis6-01nosql database
Basic knowledge of tuples
Enterprise level: spire Office for . NET:Platinum|7.7. x
error Couldn‘t find a package.json file in “你的路径“
Clickhouse materialized view
KVM virtualization
An elegant program for Euclid‘s algorithm
[web Audit - source code disclosure] obtain source code methods and use tools
程序员的视力怎么样? | 每日趣闻
It took two nights to get Wu Enda's machine learning course certificate from Stanford University
英语必备词汇3400
DECLARE_ WAIT_ QUEUE_ HEAD、wake_ up_ Interruptible macro analysis
The architect started to write a HelloWorld
@Transactional 注解导致跨库查询失效的问题
Kubernetes - Multi cluster management
25K 入职腾讯的那天,我特么哭了
一文带你了解BI的前世今身与企业数字化转型的关系
In MySQL Association query, the foreign key is null. What if the data cannot be found?
Deep learning - LSTM Foundation
[Chongqing Guangdong education] 2777t green space planning reference questions of National Open University in autumn 2018