当前位置:网站首页>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
边栏推荐
- 测试开发是什么?为什么现在那么多公司都要招聘测试开发?
- 面试字节,过关斩将直接干到 3 面,结果找了个架构师来吊打我?
- glibc strlen 实现方式分析
- Special Edition: spreadjs v15.1 vs spreadjs v15.0
- 汇编-入门
- Redis之Jedis如何使用
- Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
- Solve the problem that sqlyog does not have a schema Designer
- 深度学习——LSTM基础
- Technology sharing swift defense programming
猜你喜欢
![[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)](/img/99/bb05b6c48a9e70ca7ff77733d954b9.jpg)
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)

UI automation test farewell to manual download of browser driver

IronXL for .NET 2022.6

A brief introduction to the behavior tree of unity AI

Deep learning - LSTM Foundation

Clickhouse synchronization MySQL (based on materialization engine)

测试开发是什么?为什么现在那么多公司都要招聘测试开发?

特殊版:SpreadJS v15.1 VS SpreadJS v15.0

企业级:Spire.Office for .NET:Platinum|7.7.x

Use of vscode software
随机推荐
优先使用对象组合,而不是类继承
glibc strlen 实现方式分析
Containerization Foundation
Difference between MotionEvent. getRawX and MotionEvent. getX
[数组]566. 重塑矩阵-简单
Nmap使用手册学习记录
JWT vulnerability recurrence
Clickhouse同步mysql(基于物化引擎)
Nmap user manual learning records
[groovy] string (string injection function | asBoolean | execute | minus)
[C language] address book - dynamic and static implementation
[web Audit - source code disclosure] obtain source code methods and use tools
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
UI automation test farewell to manual download of browser driver
Multimedia query
Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
MySQL winter vacation self-study 2022 11 (10)
Analysis of glibc strlen implementation mode
error Couldn‘t find a package.json file in “你的路径“