Favoring Composition Over Inheritance
Throughout the past chapters, we have seen a recurrent topic: the idea that we should favor composition over inheritance is a fundamental idea to keep in mind when developing a good reusable code base. Weirdly, we have a fundamental object-oriented programming (OOP) pillar for inheritance, but we don’t have one for composition, right?
In OOP, one effective design strategy is to favor composition over inheritance. This approach suggests building objects using combinations of smaller, reusable components instead of forming rigid class hierarchies through inheritance. Composition offers a range of benefits, such as increased flexibility, simpler code maintenance, and dynamic adaptability during runtime. This chapter will explore why composition often provides a more modular and adaptable framework, especially useful in scenarios where requirements might evolve during production.
Inheritance has been a fundamental aspect of OOP, but it...