Unique Tutorial: Design Patterns in Python

Introduction to Design Patterns in Python

Design patterns are reusable solutions to commonly occurring problems in software design. They provide a structured approach to solving problems, making code more maintainable, flexible, and efficient. In this tutorial, we will explore some popular design patterns and how they can be implemented in Python. Let’s dive in!

Factory Pattern

The Factory pattern is a creational design pattern that provides an interface for creating objects without specifying their exact classes. This pattern is useful when you want to decouple the client code from the concrete class implementation.

class Vehicle:
    def __init__(self, model):
        self.model = model

    def drive(self):

class Car(Vehicle):
    def drive(self):
        print(f"{self.model} car is driving.")

class Bike(Vehicle):
    def drive(self):
        print(f"{self.model} bike is driving.")

class VehicleFactory:
    def create_vehicle(vehicle_type, model):
        if vehicle_type == "car":
            return Car(model)
        elif vehicle_type == "bike":
            return Bike(model)

# Usage
vehicle_factory = VehicleFactory()
car = vehicle_factory.create_vehicle("car", "Sedan")
bike = vehicle_factory.create_vehicle("bike", "Mountain")  # Sedan car is driving.  # Mountain bike is driving.

In the above example, the VehicleFactory class acts as the factory that creates different types of vehicles based on the provided vehicle_type. The client code can obtain the desired vehicle object without being aware of the underlying implementation.

Observer Pattern

The Observer pattern is a behavioral design pattern that establishes a one-to-many dependency between objects. When a subject object changes its state, all its observers are notified and updated automatically. This pattern is commonly used in event-driven systems.

class Subject:
    def __init__(self):
        self.observers = []

    def attach(self, observer):

    def detach(self, observer):

    def notify(self, data):
        for observer in self.observers:

class Observer:
    def update(self, data):

class EmailNotifier(Observer):
    def update(self, data):
        print(f"Email notification sent: {data}")

class SMSNotifier(Observer):
    def update(self, data):
        print(f"SMS notification sent: {data}")

# Usage
subject = Subject()
email_notifier = EmailNotifier()
sms_notifier = SMSNotifier()


subject.notify("Important message") 
# Email notification sent: Important message
# SMS notification sent: Important message


subject.notify("Another message")
# Email notification sent: Another message

The Subject class is the subject that maintains a list of observers and sends notifications to them when its state changes. The Observer interface defines the update method that concrete observers must implement. In this example, we have two concrete observers, EmailNotifier and SMSNotifier, which send notifications via email and SMS respectively.


Design patterns provide proven solutions to recurring problems in software development. Mastering design patterns can greatly enhance your programming skills and make your code more robust and maintainable. In this tutorial, we explored the Factory and Observer patterns in Python, but there are many more design patterns that you can explore. Happy coding!

comments powered by Disqus