当前位置:网站首页>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

原网站

版权声明
本文为[Bouncing bit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050335507752.html