Lesson 4
Revising Basic Design Patterns: Composition in Kotlin
Lesson Overview and Goals

Hello, welcome back! Today, we will decode the fundamentals of Revising Basic Design PatternsComposition! 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.

Garnering Clarity on the Composition Design Pattern

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.

Acing the Composition Design in Kotlin

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 Car class owns these child objects; their existence depends on the Car.

Kotlin
1class Engine { 2 fun start() { 3 println("Engine starts") // Engine start message 4 } 5 6 fun stop() { 7 println("Engine stops") // Engine stop message 8 } 9} 10 11class Wheels { 12 fun rotate() { 13 println("Wheels rotate") // Wheel rotation message 14 } 15} 16 17class Seats { 18 fun adjust(position: String) { 19 println("Seats adjusted to position $position") // Seat adjustment message 20 } 21} 22 23class Car(private val engine: Engine, private val wheels: Wheels, private val seats: Seats) { 24 25 fun start() { 26 engine.start() // Call to start engine 27 seats.adjust("upright") // Adjust seat position 28 wheels.rotate() // Get wheels rolling 29 } 30} 31 32fun main() { 33 val engine = Engine() 34 val wheels = Wheels() 35 val seats = Seats() 36 val myCar = Car(engine, wheels, seats) 37 myCar.start() // Begin car functions 38} 39// Prints: 40// Engine starts 41// Seats adjusted to position upright 42// Wheels rotate

In the above code, the Car class encapsulates Engine, Wheels, and Seats objects, which are independent but part of the Car class, forming a composition pattern.

In the composition model, components like Engine, Wheels, and Seats can exist independently. These classes are not subclasses of Car, but are simply used by the Car to achieve its functionality. This is an important feature of composition because it allows for the reuse of these components in other contexts. For instance, the Engine class could be used in other vehicles like a Truck or Boat, without needing to inherit from a specific Car class.

Discerning Composition from Inheritance

In OOP, Composition and Inheritance are two significant ways to express relationships between classes. While inheritance implies an "is-a" relationship, composition suggests a "has-a" relationship. For instance, a Car is a Vehicle (inheritance), but a Car has an Engine (composition).

Lesson Summary and Practice

Superb job! You've now decoded the composition and even implemented it in Kotlin! Next, you'll encounter stimulating exercises where you'll gain hands-on experience with composition in Kotlin. Stay curious, and keep practicing to fortify your concepts!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.