Python - Design Patterns

Python - Design Patterns

Design Patterns in Python

Design patterns are typical solutions to common software design problems. They are best practices adopted by experienced developers to solve recurring design issues. Python, being a dynamic and versatile language, supports multiple paradigms and allows easy implementation of various design patterns.

This document introduces key design patterns grouped into three main categories:

  • Creational Patterns
  • Structural Patterns
  • Behavioral Patterns

Each section provides explanations, use cases, and Python implementations.

Creational Design Patterns

Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could lead to design problems or added complexity. Creational patterns solve this problem by controlling the object creation process.

1. Singleton Pattern

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

class Singleton:
    _instance = None

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

obj1 = Singleton()
obj2 = Singleton()
print(obj1 is obj2)  # True

2. Factory Pattern

The Factory Method pattern defines an interface for creating objects, but allows subclasses to alter the type of objects that will be created.

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

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

def get_pet(pet="dog"):
    pets = dict(dog=Dog(), cat=Cat())
    return pets[pet]

pet = get_pet("cat")
print(pet.speak())

3. Abstract Factory Pattern

This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

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

class DogFactory:
    def get_pet(self):
        return Dog()

class PetStore:
    def __init__(self, pet_factory):
        self.pet = pet_factory.get_pet()

    def show_pet(self):
        return self.pet.speak()

store = PetStore(DogFactory())
print(store.show_pet())

4. Builder Pattern

The Builder pattern separates the construction of a complex object from its representation.

class Car:
    def __init__(self):
        self.make = None
        self.model = None

class CarBuilder:
    def __init__(self):
        self.car = Car()

    def set_make(self, make):
        self.car.make = make
        return self

    def set_model(self, model):
        self.car.model = model
        return self

    def build(self):
        return self.car

builder = CarBuilder()
car = builder.set_make("Toyota").set_model("Camry").build()
print(car.make, car.model)

Structural Design Patterns

Structural patterns deal with object composition. They help ensure that if one part of a system changes, the entire structure does not need to do the same. These patterns help ensure a flexible and scalable architecture.

1. Adapter Pattern

This pattern allows objects with incompatible interfaces to work together by wrapping their own interface around that of an already existing class.

class EuropeanSocketInterface:
    def voltage(self):
        return 230

class USASocket:
    def get_voltage(self):
        return 120

class Adapter(EuropeanSocketInterface):
    def __init__(self, usa_socket):
        self.usa_socket = usa_socket

    def voltage(self):
        return self.usa_socket.get_voltage()

adapter = Adapter(USASocket())
print(adapter.voltage())

2. Decorator Pattern

This pattern attaches additional responsibilities to an object dynamically.

def make_bold(fn):
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped

@make_bold
def greet():
    return "Hello"

print(greet())

3. Proxy Pattern

The Proxy pattern provides a surrogate or placeholder for another object to control access to it.

class RealSubject:
    def request(self):
        return "RealSubject: Handling request."

class Proxy:
    def __init__(self):
        self._real_subject = RealSubject()

    def request(self):
        print("Proxy: Logging request...")
        return self._real_subject.request()

proxy = Proxy()
print(proxy.request())

4. Facade Pattern

The Facade pattern provides a simplified interface to a complex subsystem.

class CPU:
    def freeze(self):
        print("Freezing CPU")

    def jump(self, pos):
        print(f"Jumping to {pos}")

class Memory:
    def load(self, position, data):
        print(f"Loading {data} at {position}")

class Computer:
    def __init__(self):
        self.cpu = CPU()
        self.memory = Memory()

    def start(self):
        self.cpu.freeze()
        self.memory.load("0x00", "OS")
        self.cpu.jump("0x00")

computer = Computer()
computer.start()

Behavioral Design Patterns

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. They help in defining how objects interact in a clean and organized way.

1. Observer Pattern

This pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.

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

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

    def notify_all(self, message):
        for obs in self._observers:
            obs.notify(message)

class Observer:
    def notify(self, message):
        print(f"Received: {message}")

subject = Subject()
observer1 = Observer()
observer2 = Observer()
subject.register(observer1)
subject.register(observer2)
subject.notify_all("Hello Observers!")

2. Strategy Pattern

This pattern enables selecting an algorithm’s behavior at runtime.

class TextFormatter:
    def format(self, text):
        raise NotImplementedError

class UpperCaseFormatter(TextFormatter):
    def format(self, text):
        return text.upper()

class LowerCaseFormatter(TextFormatter):
    def format(self, text):
        return text.lower()

class TextEditor:
    def __init__(self, formatter):
        self.formatter = formatter

    def publish(self, text):
        return self.formatter.format(text)

editor = TextEditor(UpperCaseFormatter())
print(editor.publish("Hello World"))

3. Command Pattern

This pattern turns a request into a stand-alone object that contains all information about the request.

class Light:
    def on(self):
        print("Light on")

    def off(self):
        print("Light off")

class Command:
    def execute(self):
        pass

class OnCommand(Command):
    def __init__(self, light):
        self.light = light

    def execute(self):
        self.light.on()

class OffCommand(Command):
    def __init__(self, light):
        self.light = light

    def execute(self):
        self.light.off()

light = Light()
on_cmd = OnCommand(light)
off_cmd = OffCommand(light)

on_cmd.execute()
off_cmd.execute()

4. Chain of Responsibility

This pattern lets multiple objects handle a request without knowing the handler.

class Handler:
    def __init__(self, successor=None):
        self.successor = successor

    def handle(self, request):
        handled = self.process(request)
        if not handled and self.successor:
            self.successor.handle(request)

    def process(self, request):
        raise NotImplementedError

class ConcreteHandler1(Handler):
    def process(self, request):
        if request < 10:
            print(f"Handler1 handled: {request}")
            return True
        return False

class ConcreteHandler2(Handler):
    def process(self, request):
        if request < 20:
            print(f"Handler2 handled: {request}")
            return True
        return False

h2 = ConcreteHandler2()
h1 = ConcreteHandler1(h2)

h1.handle(5)
h1.handle(15)
h1.handle(25)

Design patterns are fundamental building blocks in software architecture. In Python, their implementation is often cleaner due to dynamic typing and built-in features like decorators and first-class functions. Understanding design patterns enhances your code quality, improves readability, and fosters better maintainability.

This guide covered commonly used patterns across creational, structural, and behavioral categories. Patterns like Singleton, Factory, Observer, Decorator, and Strategy are especially common in Python development in frameworks such as Django, Flask, and more.

As you work on more complex systems, recognizing where and how to use these patterns will give you a solid edge in building robust software.

Beginner 5 Hours
Python - Design Patterns

Design Patterns in Python

Design patterns are typical solutions to common software design problems. They are best practices adopted by experienced developers to solve recurring design issues. Python, being a dynamic and versatile language, supports multiple paradigms and allows easy implementation of various design patterns.

This document introduces key design patterns grouped into three main categories:

  • Creational Patterns
  • Structural Patterns
  • Behavioral Patterns

Each section provides explanations, use cases, and Python implementations.

Creational Design Patterns

Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could lead to design problems or added complexity. Creational patterns solve this problem by controlling the object creation process.

1. Singleton Pattern

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

class Singleton: _instance = None def __new__(cls): if not cls._instance: cls._instance = super().__new__(cls) return cls._instance obj1 = Singleton() obj2 = Singleton() print(obj1 is obj2) # True

2. Factory Pattern

The Factory Method pattern defines an interface for creating objects, but allows subclasses to alter the type of objects that will be created.

class Dog: def speak(self): return "Woof!" class Cat: def speak(self): return "Meow!" def get_pet(pet="dog"): pets = dict(dog=Dog(), cat=Cat()) return pets[pet] pet = get_pet("cat") print(pet.speak())

3. Abstract Factory Pattern

This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

class Dog: def speak(self): return "Woof!" class DogFactory: def get_pet(self): return Dog() class PetStore: def __init__(self, pet_factory): self.pet = pet_factory.get_pet() def show_pet(self): return self.pet.speak() store = PetStore(DogFactory()) print(store.show_pet())

4. Builder Pattern

The Builder pattern separates the construction of a complex object from its representation.

class Car: def __init__(self): self.make = None self.model = None class CarBuilder: def __init__(self): self.car = Car() def set_make(self, make): self.car.make = make return self def set_model(self, model): self.car.model = model return self def build(self): return self.car builder = CarBuilder() car = builder.set_make("Toyota").set_model("Camry").build() print(car.make, car.model)

Structural Design Patterns

Structural patterns deal with object composition. They help ensure that if one part of a system changes, the entire structure does not need to do the same. These patterns help ensure a flexible and scalable architecture.

1. Adapter Pattern

This pattern allows objects with incompatible interfaces to work together by wrapping their own interface around that of an already existing class.

class EuropeanSocketInterface: def voltage(self): return 230 class USASocket: def get_voltage(self): return 120 class Adapter(EuropeanSocketInterface): def __init__(self, usa_socket): self.usa_socket = usa_socket def voltage(self): return self.usa_socket.get_voltage() adapter = Adapter(USASocket()) print(adapter.voltage())

2. Decorator Pattern

This pattern attaches additional responsibilities to an object dynamically.

def make_bold(fn): def wrapped(): return "<b>" + fn() + "</b>" return wrapped @make_bold def greet(): return "Hello" print(greet())

3. Proxy Pattern

The Proxy pattern provides a surrogate or placeholder for another object to control access to it.

class RealSubject: def request(self): return "RealSubject: Handling request." class Proxy: def __init__(self): self._real_subject = RealSubject() def request(self): print("Proxy: Logging request...") return self._real_subject.request() proxy = Proxy() print(proxy.request())

4. Facade Pattern

The Facade pattern provides a simplified interface to a complex subsystem.

class CPU: def freeze(self): print("Freezing CPU") def jump(self, pos): print(f"Jumping to {pos}") class Memory: def load(self, position, data): print(f"Loading {data} at {position}") class Computer: def __init__(self): self.cpu = CPU() self.memory = Memory() def start(self): self.cpu.freeze() self.memory.load("0x00", "OS") self.cpu.jump("0x00") computer = Computer() computer.start()

Behavioral Design Patterns

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. They help in defining how objects interact in a clean and organized way.

1. Observer Pattern

This pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.

class Subject: def __init__(self): self._observers = [] def register(self, observer): self._observers.append(observer) def notify_all(self, message): for obs in self._observers: obs.notify(message) class Observer: def notify(self, message): print(f"Received: {message}") subject = Subject() observer1 = Observer() observer2 = Observer() subject.register(observer1) subject.register(observer2) subject.notify_all("Hello Observers!")

2. Strategy Pattern

This pattern enables selecting an algorithm’s behavior at runtime.

class TextFormatter: def format(self, text): raise NotImplementedError class UpperCaseFormatter(TextFormatter): def format(self, text): return text.upper() class LowerCaseFormatter(TextFormatter): def format(self, text): return text.lower() class TextEditor: def __init__(self, formatter): self.formatter = formatter def publish(self, text): return self.formatter.format(text) editor = TextEditor(UpperCaseFormatter()) print(editor.publish("Hello World"))

3. Command Pattern

This pattern turns a request into a stand-alone object that contains all information about the request.

class Light: def on(self): print("Light on") def off(self): print("Light off") class Command: def execute(self): pass class OnCommand(Command): def __init__(self, light): self.light = light def execute(self): self.light.on() class OffCommand(Command): def __init__(self, light): self.light = light def execute(self): self.light.off() light = Light() on_cmd = OnCommand(light) off_cmd = OffCommand(light) on_cmd.execute() off_cmd.execute()

4. Chain of Responsibility

This pattern lets multiple objects handle a request without knowing the handler.

class Handler: def __init__(self, successor=None): self.successor = successor def handle(self, request): handled = self.process(request) if not handled and self.successor: self.successor.handle(request) def process(self, request): raise NotImplementedError class ConcreteHandler1(Handler): def process(self, request): if request < 10: print(f"Handler1 handled: {request}") return True return False class ConcreteHandler2(Handler): def process(self, request): if request < 20: print(f"Handler2 handled: {request}") return True return False h2 = ConcreteHandler2() h1 = ConcreteHandler1(h2) h1.handle(5) h1.handle(15) h1.handle(25)

Design patterns are fundamental building blocks in software architecture. In Python, their implementation is often cleaner due to dynamic typing and built-in features like decorators and first-class functions. Understanding design patterns enhances your code quality, improves readability, and fosters better maintainability.

This guide covered commonly used patterns across creational, structural, and behavioral categories. Patterns like Singleton, Factory, Observer, Decorator, and Strategy are especially common in Python development in frameworks such as Django, Flask, and more.

As you work on more complex systems, recognizing where and how to use these patterns will give you a solid edge in building robust software.

Frequently Asked Questions for Python

Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.


Python's syntax is a lot closer to English and so it is easier to read and write, making it the simplest type of code to learn how to write and develop with. The readability of C++ code is weak in comparison and it is known as being a language that is a lot harder to get to grips with.

Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works. Performance: Java has a higher performance than Python due to its static typing and optimization by the Java Virtual Machine (JVM).

Python can be considered beginner-friendly, as it is a programming language that prioritizes readability, making it easier to understand and use. Its syntax has similarities with the English language, making it easy for novice programmers to leap into the world of development.

To start coding in Python, you need to install Python and set up your development environment. You can download Python from the official website, use Anaconda Python, or start with DataLab to get started with Python in your browser.

Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.

Python alone isn't going to get you a job unless you are extremely good at it. Not that you shouldn't learn it: it's a great skill to have since python can pretty much do anything and coding it is fast and easy. It's also a great first programming language according to lots of programmers.

The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.


Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.


6 Top Tips for Learning Python

  • Choose Your Focus. Python is a versatile language with a wide range of applications, from web development and data analysis to machine learning and artificial intelligence.
  • Practice regularly.
  • Work on real projects.
  • Join a community.
  • Don't rush.
  • Keep iterating.

The following is a step-by-step guide for beginners interested in learning Python using Windows.

  • Set up your development environment.
  • Install Python.
  • Install Visual Studio Code.
  • Install Git (optional)
  • Hello World tutorial for some Python basics.
  • Hello World tutorial for using Python with VS Code.

Best YouTube Channels to Learn Python

  • Corey Schafer.
  • sentdex.
  • Real Python.
  • Clever Programmer.
  • CS Dojo (YK)
  • Programming with Mosh.
  • Tech With Tim.
  • Traversy Media.

Python can be written on any computer or device that has a Python interpreter installed, including desktop computers, servers, tablets, and even smartphones. However, a laptop or desktop computer is often the most convenient and efficient option for coding due to its larger screen, keyboard, and mouse.

Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.

  • Google's Python Class.
  • Microsoft's Introduction to Python Course.
  • Introduction to Python Programming by Udemy.
  • Learn Python - Full Course for Beginners by freeCodeCamp.
  • Learn Python 3 From Scratch by Educative.
  • Python for Everybody by Coursera.
  • Learn Python 2 by Codecademy.

  • Understand why you're learning Python. Firstly, it's important to figure out your motivations for wanting to learn Python.
  • Get started with the Python basics.
  • Master intermediate Python concepts.
  • Learn by doing.
  • Build a portfolio of projects.
  • Keep challenging yourself.

Top 5 Python Certifications - Best of 2024
  • PCEP (Certified Entry-level Python Programmer)
  • PCAP (Certified Associate in Python Programmer)
  • PCPP1 & PCPP2 (Certified Professional in Python Programming 1 & 2)
  • Certified Expert in Python Programming (CEPP)
  • Introduction to Programming Using Python by Microsoft.

The average salary for Python Developer is ₹5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from ₹3,000 - ₹1,20,000.

The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python website, https://www.python.org/, and may be freely distributed.

If you're looking for a lucrative and in-demand career path, you can't go wrong with Python. As one of the fastest-growing programming languages in the world, Python is an essential tool for businesses of all sizes and industries. Python is one of the most popular programming languages in the world today.

line

Copyrights © 2024 letsupdateskills All rights reserved