Hello, welcome back! Today, we will decode the fundamentals of Revising Basic Design Patterns — Composition! A vital component of software design patterns, composition aids us in creating complex classes using simpler ones. Our journey today includes understanding the concept of composition, its value in software development, and how to practically implement it in Kotlin.
To kick-start our exploration, let's understand Composition
. In object-oriented programming (OOP), composition allows a class to include other classes, paving the way for creating complex systems out of simpler components. For instance, when building a car, we bring together independent pieces like the Engine
, Wheels
, and Seats
— a perfect reflection of composition in everyday life. Note that in composition, if the parent object (the car) is destroyed, the child objects (the components) also cease to exist.
Now, let's translate the theory into a Kotlin code application. Transforming the previously mentioned car example, a Car
class in Kotlin is created by making objects of the Engine
, Wheels
, and Seats
classes. The class owns these child objects; their existence depends on the .
