Python

 

 

Introduction

Object-Oriented Design Patterns (OODP) are essential tools in software development that solve common problems in software architecture. They help in creating more scalable, maintainable, and flexible code. By leveraging design patterns in Python, developers can create more structured and efficient programs. In this tutorial, we will explore some popular design patterns in Python, such as Singleton, Factory, and Observer, through clear code examples.

Objective

The objective of this topic is to:

  • Introduce the concept of Object-Oriented Design Patterns.
  • Provide practical Python code examples for each pattern.
  • Explain how these patterns improve software design and solve common problems.

Example 1: Singleton Pattern

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it.


class Singleton:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

# Testing Singleton
singleton1 = Singleton()
singleton2 = Singleton()

print(singleton1 == singleton2)  # Output: True, both are the same instance
        

Explanation of Singleton

The Singleton class ensures that only one instance of the class is created. The __new__ method checks whether an instance already exists. If not, it creates one. Every subsequent call to create a new Singleton object will return the same instance.

Example 2: Factory Pattern

The Factory Pattern is used to create objects without specifying the exact class of the object that will be created.


class Dog:
    def speak(self):
        return "Woof!"

class Cat:
    def speak(self):
        return "Meow!"

class AnimalFactory:
    def get_animal(self, animal_type):
        if animal_type == "dog":
            return Dog()
        elif animal_type == "cat":
            return Cat()
        else:
            raise ValueError("Unknown animal type!")

# Testing Factory Pattern
factory = AnimalFactory()
animal1 = factory.get_animal("dog")
animal2 = factory.get_animal("cat")

print(animal1.speak())  # Output: Woof!
print(animal2.speak())  # Output: Meow!
        

Explanation of Factory

The AnimalFactory class abstracts the creation of objects. It provides a get_animal method that returns an instance of Dog or Cat based on the input. This pattern helps in encapsulating the logic of creating objects, making it easier to manage and extend.

Example 3: Observer Pattern

The Observer Pattern is a behavioral pattern where an object (the subject) maintains a list of its dependents (observers) and notifies them of any state changes.


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

    def add_observer(self, observer):
        self._observers.append(observer)

    def notify(self):
        for observer in self._observers:
            observer.update()

class Observer:
    def update(self):
        raise NotImplementedError("Subclass must implement abstract method")

class ConcreteObserver(Observer):
    def update(self):
        print("Observer notified!")

# Testing Observer Pattern
subject = Subject()
observer1 = ConcreteObserver()
observer2 = ConcreteObserver()

subject.add_observer(observer1)
subject.add_observer(observer2)

subject.notify()  # Output: Observer notified! Observer notified!
        

Explanation of Observer

The Subject class maintains a list of observers and provides a notify method to update them. The Observer class defines an update method that is called when the subject’s state changes. This pattern is useful for implementing event-driven systems.

How to Run the Program

To run the above examples, follow these steps:

  1. Ensure you have Python installed (version 3.x).
  2. Copy the code snippets into a text file with a .py extension (e.g., design_patterns.py).
  3. Open a terminal and navigate to the folder where your Python file is saved.
  4. Run the program using the command: python design_patterns.py
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)