当前位置:网站首页>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
边栏推荐
- How to define a unified response object gracefully
- [array]566 Reshape the matrix - simple
- 灵魂三问:什么是接口测试,接口测试怎么玩,接口自动化测试怎么玩?
- 特殊版:SpreadJS v15.1 VS SpreadJS v15.0
- provide/inject
- 【刷题】BFS题目精选
- [wp]bmzclub writeup of several questions
- Redis6-01nosql database
- 为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
- JWT vulnerability recurrence
猜你喜欢

Use of vscode software

线程基础知识

Multimedia query
![[software reverse - basic knowledge] analysis method, assembly instruction architecture](/img/97/8001db1c572495a115d32d9dd7360e.png)
[software reverse - basic knowledge] analysis method, assembly instruction architecture

How about programmers' eyesight| Daily anecdotes

面试字节,过关斩将直接干到 3 面,结果找了个架构师来吊打我?

Redis之Jedis如何使用

为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?

About the recent experience of writing questions

v-if VS v-show 2.0
随机推荐
NEW:Devart dotConnect ADO. NET
KVM virtualization
51 independent key basic experiment
面试汇总:这是一份全面&详细的Android面试指南
为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
Analysis of glibc strlen implementation mode
Anti debugging (basic principles of debugger Design & NT NP and other anti debugging principles)
Basic function learning 02
ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
Nmap使用手册学习记录
【web審計-源碼泄露】獲取源碼方法,利用工具
[untitled]
[wp]bmzclub几道题的writeup
【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
v-if VS v-show 2.0
UI自動化測試從此告別手動下載瀏覽器驅動
About MySQL database connection exceptions
JWT漏洞复现
[wp][入门]刷弱类型题目
A brief introduction to the behavior tree of unity AI