---
title: "Design Patterns in Python: Singleton, Factory, and Observer Explained with Examples"
description: "Learn when to use Singleton, Factory, and Observer design patterns in Python. Practical examples show how these patterns reduce complexity in real codebases—and when to skip them to avoid overengineering."
image: "https://www.sethserver.com/static/images/python-design-patterns-implementing-elegant-solutions-to-common-problems-1-1.webp"
---

# Design Patterns in Python: Singleton, Factory, and Observer Explained with Examples

You can usually tell when a codebase needs design patterns because everyone starts inventing their own. You'll see three different "event systems," two "config loaders," and one brave soul who passed a database connection through 19 function calls like a cursed relay baton.

Design patterns aren't magical. They're just names for shapes that show up over and over. If you've got enough curiosity (and enough bugs), you start to recognize the shapes. Then system design gets simpler: you recognize the shape, you know what it does, and you know whether it fits your problem.

Below are three patterns I actually see in Python code that ships: **Singleton**, **Factory**, and **Observer**. They can make your code cleaner. They can also make it worse. Both outcomes are very achievable.

## Design patterns are templates, not collectibles

A pattern is not an off-the-shelf component. It's a reusable plan.

The trap is treating patterns like Pokémon. "Gotta catch 'em all." That's how you end up with a tiny script that has AbstractBaseFactories and a ServiceLocator and a sad little TODO file begging for mercy.

Use patterns when they remove friction:
- fewer "special cases"
- clearer ownership of state
- fewer spaghetti dependencies
- easier testing (yes, sometimes)

## Singleton: one instance, one source of truth

Singleton is the "there should only be one of these" pattern. Common targets:
- config managers
- shared caches
- connection pools (careful here)
- a central "data manager" object

A typical Python way is overriding `__new__`:

```python
class Config:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance.values = {}
        return cls._instance

c1 = Config()
c2 = Config()
assert c1 is c2
```

**Warning:** Singleton is global state with a nicer outfit. Overuse it and your tests get weird fast. You'll also couple code that shouldn't know about each other.

I once debugged a system where someone had created five different Singleton implementations. Each one thought it was the only one. The irony was not lost on me, but the three hours weren't funny.

If you can pass an object as a parameter (or, gasp, use dependency injection), do that first. Singleton is for the cases where passing it around turns your function signatures into a phone book.

## Factory: centralize object creation without hard-coding classes everywhere

Factories help when you need to create different concrete types behind a shared interface. Example: a game enemy system.

```python
from abc import ABC, abstractmethod

class Enemy(ABC):
    @abstractmethod
    def attack(self) -> str: ...

class Orc(Enemy):
    def attack(self) -> str:
        return "Orc swings a club"

class Dragon(Enemy):
    def attack(self) -> str:
        return "Dragon breathes fire"

class EnemyFactory:
    @staticmethod
    def create_enemy(enemy_type: str) -> Enemy:
        if enemy_type == "orc":
            return Orc()
        if enemy_type == "dragon":
            return Dragon()
        raise ValueError(f"Unknown enemy_type={enemy_type}")
```

This is useful when the type is decided at runtime (config, user input, feature flags). It also keeps creation logic in one place.

**Warning:** Don't add a factory just because you can. If you have two classes and they never change, a factory is just extra indirection. This is a classic case of [best practices becoming antipatterns](https://www.sethserver.com/startups/best-practices-antipatterns-and-why-nothing-seems-to-work.html) when applied without thought.

## Observer: subscriptions and notifications without tight coupling

Observer is the "tell everyone who cares" pattern. GUI frameworks use it. Event systems use it. Anything reactive uses it.

```python
class Subject:
    def __init__(self):
        self._observers = []
        self._state = None

    def attach(self, obs):
        self._observers.append(obs)

    def detach(self, obs):
        self._observers.remove(obs)

    def notify(self):
        for obs in list(self._observers):
            obs.update(self._state)

    def set_state(self, state):
        self._state = state
        self.notify()

class Observer:
    def update(self, state): ...

class PrintObserver(Observer):
    def update(self, state):
        print(f"state changed: {state}")
```

**Warning:** Observer can become a performance problem if you notify constantly or you have lots of observers. Also watch lifetimes. If you keep references to observers forever, you've invented a memory leak with extra steps.

## A real combined example: weather monitoring

Here's a small system that uses all three patterns together:
- **Singleton**: one `WeatherDataManager`
- **Factory**: create stations based on type
- **Observer**: displays subscribe to updates

```python
from abc import ABC, abstractmethod

class WeatherStation(ABC):
    @abstractmethod
    def collect_data(self) -> dict: ...

class BasicWeatherStation(WeatherStation):
    def collect_data(self) -> dict:
        return {"temp": 20, "humidity": 50, "pressure": 1013}

class AdvancedWeatherStation(WeatherStation):
    def collect_data(self) -> dict:
        return {"temp": 20.5, "humidity": 49.8, "pressure": 1012.8}

class WeatherStationFactory:
    @staticmethod
    def create(station_type: str) -> WeatherStation:
        if station_type == "basic":
            return BasicWeatherStation()
        if station_type == "advanced":
            return AdvancedWeatherStation()
        raise ValueError(station_type)

class WeatherDisplay(ABC):
    @abstractmethod
    def update(self, data: dict): ...

class TemperatureDisplay(WeatherDisplay):
    def update(self, data: dict):
        print(f"Temp: {data['temp']}")

class HumidityDisplay(WeatherDisplay):
    def update(self, data: dict):
        print(f"Humidity: {data['humidity']}")

class WeatherDataManager:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._observers = []
            cls._instance._data = {}
        return cls._instance

    def attach(self, display: WeatherDisplay):
        self._observers.append(display)

    def notify(self):
        for obs in list(self._observers):
            obs.update(self._data)

    def ingest(self, data: dict):
        self._data = data
        self.notify()

## wiring
manager = WeatherDataManager()
manager.attach(TemperatureDisplay())
manager.attach(HumidityDisplay())

station = WeatherStationFactory.create("advanced")
manager.ingest(station.collect_data())
```

That's the whole machine: one manager, swappable input sources, and decoupled outputs.

Use patterns when they make the code simpler. Skip them when they don't. That's the whole game.

-Sethers


```json
{"@context": "https://schema.org", "@type": "BlogPosting", "headline": "Design Patterns in Python: Singleton, Factory, and Observer Explained with Examples", "datePublished": "2024-09-30T00:43:32", "dateModified": "2026-03-18T23:41:37", "image": {"@type": "ImageObject", "url": "https://www.sethserver.com/static/images/python-design-patterns-implementing-elegant-solutions-to-common-problems-1-1.webp", "width": 400, "height": 400}, "url": "https://www.sethserver.com/programming/python-design-patterns-implementing-elegant-solutions-to-common-problems.html", "author": {"@type": "Person", "name": "Seth Black", "url": "https://www.linkedin.com/in/seth-black-tx"}, "publisher": {"@type": "Organization", "name": "Sethserver", "logo": {"@type": "ImageObject", "url": "https://www.sethserver.com/static/images/PaperAirplane512.png"}}, "description": "Learn when to use Singleton, Factory, and Observer design patterns in Python. Practical examples show how these patterns reduce complexity in real codebases—and when to skip them to avoid overengineering.", "articleSection": "Technology", "articleBody": "In the world of software development, design patterns are like the secret recipes of master chefs. They're not just about making code work; they're about making it work elegantly, efficiently, and in a way that stands the test of time. As a Python developer, understanding these patterns can elevate your code from functional to fantastic.  Design patterns are tried-and-true solutions to common problems that arise in software design. They're not off-the-shelf components that you can plug into your code, but rather templates that you can adapt to solve specific issues in your projects. Think of them as best practices that have evolved over time, distilled from the collective experience of countless developers who've faced similar challenges.  In this post, we'll explore three fundamental design patterns: Singleton, Factory, and Observer. We'll dive into their implementations in Python, discuss when to use them, and examine their pros and cons. By the end, you'll have a solid grasp of these patterns and how they can make your Python code more maintainable, scalable, and elegant.  The Singleton Pattern: One Is the Loneliest Number  The Singleton pattern is perhaps the simplest of the design patterns we'll discuss, but don't let its simplicity fool you. It's a powerful tool when used correctly. The main idea behind the Singleton pattern is to ensure that a class has only one instance and to provide a global point of access to that instance.  Why would you want only one instance of a class? Think about scenarios where you need to manage shared resources or maintain a global state. For example, a configuration manager that loads settings from a file, or a connection pool for a database. In these cases, having multiple instances could lead to inconsistencies or resource conflicts.  Here's a basic implementation of the Singleton pattern in Python:  ```python class Singleton:     _instance = None      def __new__(cls):         if cls._instance is None:             cls._instance = super().__new__(cls)         return cls._instance      def some_business_logic(self):         pass ```  In this implementation, we override the `__new__` method, which is responsible for creating and returning new instances of the class. We check if an instance already exists. If not, we create one; otherwise, we return the existing instance.  Using this Singleton is straightforward:  ```python s1 = Singleton() s2 = Singleton()  print(s1 is s2)  # Output: True ```  Both `s1` and `s2` refer to the same instance, demonstrating that only one instance of the Singleton class exists.  While the Singleton pattern can be useful, it's important to use it judiciously. Overuse of global state can make your code harder to test and reason about. It can also introduce tight coupling between different parts of your application. Before reaching for the Singleton pattern, consider if there are other ways to achieve your goal, such as dependency injection or simply passing instances as parameters.  The Factory Pattern: Object Creation Made Easy  Next up is the Factory pattern, a creational pattern that provides an interface for creating objects without specifying their exact classes. This pattern is particularly useful when you have a superclass with multiple subclasses, and based on input, you need to return one of the sub-class instances.  The Factory pattern promotes loose coupling by eliminating the need for application code to specify the exact class of object that needs to be created. This can make your code more flexible and easier to extend.  Let's look at an example. Suppose we're building a game with different types of enemies:  ```python from abc import ABC, abstractmethod  class Enemy(ABC):     @abstractmethod     def attack(self):         pass  class Orc(Enemy):     def attack(self):         return \"The Orc swings its axe!\"  class Dragon(Enemy):     def attack(self):         return \"The Dragon breathes fire!\"  class EnemyFactory:     def create_enemy(self, enemy_type):         if enemy_type == \"orc\":             return Orc()         elif enemy_type == \"dragon\":             return Dragon()         else:             raise ValueError(\"Unknown enemy type\") ```  In this example, `Enemy` is an abstract base class, and `Orc` and `Dragon` are concrete implementations. The `EnemyFactory` class is responsible for creating the appropriate enemy based on the input.  Using the factory is simple:  ```python factory = EnemyFactory() orc = factory.create_enemy(\"orc\") dragon = factory.create_enemy(\"dragon\")  print(orc.attack())    # Output: The Orc swings its axe! print(dragon.attack()) # Output: The Dragon breathes fire! ```  The Factory pattern shines when you need to manage complex object creation logic. It's particularly useful in scenarios where:  1. The type of objects you need to create is determined at runtime. 2. You're working with objects that share a common interface but have different implementations. 3. You want to centralize object creation logic to make your code easier to maintain.  However, like all patterns, the Factory pattern isn't a silver bullet. For simple object creation scenarios, it might introduce unnecessary complexity. Always consider the specific needs of your project before implementing any design pattern.  The Observer Pattern: Keeping Objects in the Loop  The Observer pattern is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they're observing. It's widely used in event-driven programming and is a key part of the Model-View-Controller (MVC) architectural pattern.  In Python, we can implement the Observer pattern using classes for both the subject (the object being observed) and the observers. Here's a simple implementation:  ```python class Subject:     def __init__(self):         self._observers = []         self._state = None      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(self._state)      def set_state(self, state):         self._state = state         self.notify()  class Observer:     def update(self, state):         pass  class ConcreteObserverA(Observer):     def update(self, state):         print(f\"ConcreteObserverA: Reacted to the event. New state: {state}\")  class ConcreteObserverB(Observer):     def update(self, state):         print(f\"ConcreteObserverB: Reacted to the event. New state: {state}\") ```  In this implementation, the `Subject` class maintains a list of observers and provides methods to attach and detach observers. When its state changes, it notifies all attached observers. The `Observer` class defines an interface for objects that should be notified of changes in a Subject.  Here's how you might use this pattern:  ```python subject = Subject()  observer_a = ConcreteObserverA() subject.attach(observer_a)  observer_b = ConcreteObserverB() subject.attach(observer_b)  subject.set_state(123) # Output: # ConcreteObserverA: Reacted to the event. New state: 123 # ConcreteObserverB: Reacted to the event. New state: 123  subject.detach(observer_a) subject.set_state(456) # Output: # ConcreteObserverB: Reacted to the event. New state: 456 ```  The Observer pattern is particularly useful when you have a one-to-many relationship between objects, where a change in one object should trigger updates in multiple other objects. It's commonly used in:  1. GUI frameworks, where changes in the model need to update multiple UI elements. 2. Event handling systems, where multiple handlers need to respond to a single event. 3. Publish-subscribe systems, where publishers broadcast messages to interested subscribers.  While powerful, the Observer pattern can introduce complexity, especially when dealing with many observers or frequent updates. It's important to manage observer lifetimes carefully to avoid memory leaks or unnecessary update calls.  Putting It All Together: A Real-World Example  Now that we've explored these three design patterns individually, let's look at how they might work together in a more complex scenario. Imagine we're building a weather monitoring system that needs to create different types of weather stations, ensure there's only one central weather data manager, and notify various display elements when weather data changes.  Here's how we might implement this using the Singleton, Factory, and Observer patterns:  ```python from abc import ABC, abstractmethod  # Singleton Pattern class WeatherDataManager:     _instance = None      def __new__(cls):         if cls._instance is None:             cls._instance = super().__new__(cls)             cls._instance._observers = []             cls._instance._weather_data = {}         return cls._instance      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(self._weather_data)      def set_weather_data(self, station_id, temperature, humidity, pressure):         self._weather_data[station_id] = {             'temperature': temperature,             'humidity': humidity,             'pressure': pressure         }         self.notify()  # Factory Pattern class WeatherStation(ABC):     @abstractmethod     def collect_data(self):         pass  class BasicWeatherStation(WeatherStation):     def collect_data(self):         return {'temperature': 20, 'humidity': 50, 'pressure': 1013}  class AdvancedWeatherStation(WeatherStation):     def collect_data(self):         return {'temperature': 20.5, 'humidity': 49.8, 'pressure': 1012.8}  class WeatherStationFactory:     def create_weather_station(self, station_type):         if station_type == \"basic\":             return BasicWeatherStation()         elif station_type == \"advanced\":             return AdvancedWeatherStation()         else:             raise ValueError(\"Unknown weather station type\")  # Observer Pattern class WeatherDisplay(ABC):     @abstractmethod     def update(self, weather_data):         pass  class TemperatureDisplay(WeatherDisplay):     def update(self, weather_data):         for station_id, data in weather_data.items():             print(f\"Temperature Display - Station {station_id}: {data['temperature']}�C\")  class HumidityDisplay(WeatherDisplay):     def update(self, weather_data):         for station_id, data in weather_data.items():             print(f\"Humidity Display - Station {station_id}: {data['humidity']}%\")  # Usage if __name__ == \"__main__\":     # Create the singleton WeatherDataManager     weather_manager = WeatherDataManager()      # Create displays and attach them to the manager     temp_display = TemperatureDisplay()     humidity_display = HumidityDisplay()     weather_manager.attach(temp_display)     weather_manager.attach(humidity_display)      # Create weather stations using the factory     station_factory = WeatherStationFactory()     basic_station = station_factory.create_weather_station(\"basic\")     advanced_station = station_factory.create_weather_station(\"advanced\")      # Collect and set data from stations     basic_data = basic_station.collect_data()     weather_manager.set_weather_data(\"Station1\", **basic_data)      advanced_data = advanced_station.collect_data()     weather_manager.set_weather_data(\"Station2\", **advanced_data) ```  In this example:  1. We use the Singleton pattern for the `WeatherDataManager` to ensure there's only one central point for managing weather data. 2. The Factory pattern is used to create different types of weather stations without the main code needing to know the specific classes. 3. The Observer pattern is implemented to update various displays when the weather data changes.  This combination of patterns results in a flexible and extensible system. We can easily add new types of weather stations or displays without modifying existing code, and we ensure that all parts of the system work with a single, consistent set of weather data.  Conclusion: The Art of Pattern Application  The Singleton pattern can be invaluable for managing global state or resources, but overuse can lead to tight coupling and testing difficulties. The Factory pattern provides a flexible way to create objects, but for simple creation logic, it might be overkill. The Observer pattern is excellent for building responsive, event-driven systems, but can become complex with many observers or frequent updates.  As you continue your journey in software development, you'll encounter many more design patterns beyond these three. Each has its strengths and weaknesses, and part of becoming a skilled developer is learning to recognize when a pattern will truly benefit your code and when it might introduce unnecessary complexity. In the end, mastering design patterns is about more than just memorizing implementations. It's about understanding the problems they solve and the principles behind them. With this knowledge, you'll be well-equipped to tackle complex software design challenges and create Python code that stands the test of time. Good luck out there!  -Sethers"}
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "sethserver", "item": "https://www.sethserver.com/"}, {"@type": "ListItem", "position": 2, "name": "Programming", "item": "https://www.sethserver.com/programming/"}, {"@type": "ListItem", "position": 3, "name": "Design Patterns in Python: Singleton, Factory, and Observer Explained with Examples", "item": "https://www.sethserver.com/programming/python-design-patterns-implementing-elegant-solutions-to-common-problems.html"}]}
```
