Programming

Introduction to Design Patterns in Python

Introduction

Design patterns are reusable solutions to common programming problems. They provide a structured approach to design software and improve code reusability, maintainability, and scalability. In this tutorial, we will explore some of the most commonly used design patterns in Python and learn how to implement them in our projects.

1. Singleton Pattern

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. This pattern is helpful when you want to limit the number of instances of a class and ensure that all objects in the system share the same state.

class Singleton:
    _instance = None

    def __new__(cls):
        if not cls._instance:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        self.data = []

singleton = Singleton()

2. Observer Pattern

The Observer pattern defines a one-to-many relationship between objects, where changes in one object are reflected in other dependent objects. This pattern is useful when you want to establish communication between objects without needing them to have direct knowledge of each other.

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

    def attach(self, observer):
        self.observers.append(observer)

    def detach(self, observer):
        self.observers.remove(observer)

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

class Observer:
    def __init__(self, subject):
        self.subject = subject
        self.subject.attach(self)

    def update(self):
        print("Subject state has changed")

subject = Subject()
observer = Observer(subject)
subject.notify()

3. Factory Pattern

The Factory pattern provides an interface for creating objects without specifying their concrete classes. This pattern is useful when you want to delegate the creation of objects to a factory class, which can create objects of varying types based on input parameters.

class Shape:
    def draw(self):
        pass

class Circle(Shape):
    def draw(self):
        print("Drawing a circle")

class Rectangle(Shape):
    def draw(self):
        print("Drawing a rectangle")

class ShapeFactory:
    def create_shape(self, shape_type):
        if shape_type == "circle":
            return Circle()
        elif shape_type == "rectangle":
            return Rectangle()
        else:
            raise ValueError("Invalid shape type")

factory = ShapeFactory()
shape = factory.create_shape("circle")
shape.draw()

Conclusion

Design patterns provide solutions to common architectural problems in software development. They promote code reusability, maintainability, and scalability. By understanding and implementing design patterns, you can write cleaner, more organized, and maintainable code in Python.

comments powered by Disqus