Lesson 4

Composing Python: Building Blocks of Software Design Patterns

Lesson Overview and Goals

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 an understanding of the concept of composition, its value in software development, and how to practically implement it in Python.

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 the creation of 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, should the parent object (the car) be destroyed, the child objects (the components) also cease to exist.

Acing the Composition Design in Python

Now, let's translate the theory into a Python code application. Transforming the previously mentioned car example, a Car class in Python is created by making objects of the Engine, Wheels, and Seats classes. The Car class owns these child objects; their existence is dependent on the Car.

Python
1class Engine: 2 def start(self): 3 print("Engine starts") # Engine start message 4 5 def stop(self): 6 print("Engine stops") # Engine stop message 7 8class Wheels: 9 def rotate(self): 10 print("Wheels rotate") # Wheel rotation message 11 12class Seats: 13 def adjust(self, position): 14 print(f"Seats adjusted to position {position}") # Seat adjustment message 15 16class Car: 17 def __init__(self): 18 self.engine = Engine() 19 self.wheels = Wheels() 20 self.seats = Seats() 21 22 def start(self): 23 self.engine.start() # Call to start engine 24 self.seats.adjust('upright') # Adjust seat position 25 self.wheels.rotate() # Get wheels rolling 26 27my_car = Car() 28my_car.start() # Begin car functions 29""" 30Prints: 31Engine starts 32Seats adjusted to position upright 33Wheels rotate 34"""

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.

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 composition and even implemented it in Python! Next, you'll encounter stimulating exercises where you'll gain hands-on experience with composition in Python. 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.