python-patterns[Edit section][Copy link]
The python-patterns repository provides a collection of design pattern implementations in Python, serving as a reference and learning resource for software engineers looking to apply common design patterns in their Python projects. It addresses the challenge of understanding and implementing design patterns by providing concrete, working examples.
The repository is organized into several main directories:
-
…/behavioral
: Contains implementations of behavioral design patterns such as Observer, State, and Strategy. These patterns focus on communication between objects and the assignment of responsibilities. -
…/creational
: Includes creational patterns like Abstract Factory, Builder, and Prototype. These patterns deal with object creation mechanisms. -
…/structural
: Covers structural patterns such as Adapter, Decorator, and Proxy. These patterns are concerned with how classes and objects are composed to form larger structures. -
…/other
: Contains additional patterns and algorithms like a Hierarchical State Machine and graph search algorithms.
The implementation of each pattern typically follows this structure:
- A main class or set of classes that embody the core concept of the pattern
- Helper classes or functions that support the pattern's implementation
- A
main()
function or doctest that demonstrates the usage of the pattern
For example, the Observer pattern in …/observer.py
is implemented as follows:
- The
Data
class represents the subject being observed DecimalViewer
andHexViewer
classes act as concrete observers- The pattern's functionality is demonstrated through attaching/detaching observers and updating the subject's state
The repository also includes a test suite in the tests
directory, which verifies the correct implementation of the patterns. These tests serve as additional examples of how to use the patterns in practice.
Key design choices in the repository include:
- Use of abstract base classes and protocols to define interfaces
- Emphasis on composition over inheritance where appropriate
- Implementation of patterns using both class-based and function-based approaches
For more detailed information on specific pattern categories, refer to the following sections:
Behavioral Patterns Creational Patterns Structural Patterns Other Design Patterns
Behavioral Patterns[Edit section][Copy link]
References: patterns/behavioral
The …/behavioral
directory contains implementations of various behavioral design patterns in Python. These patterns focus on the interaction and communication between objects, as well as the flow of control within a program.
Design Pattern Implementations[Edit section][Copy link]
References: patterns/behavioral
The Behavioral Patterns directory (…/behavioral
) contains implementations of various design patterns focusing on object interactions and responsibilities.
Communication and Interaction Patterns[Edit section][Copy link]
References: patterns/behavioral/mediator.py
, patterns/behavioral/observer.py
, patterns/behavioral/publish_subscribe.py
The Mediator, Observer, and Publish-Subscribe patterns are implemented to manage communication between objects.
Read moreControl Flow Patterns[Edit section][Copy link]
References: patterns/behavioral/chain_of_responsibility.py
, patterns/behavioral/command.py
, patterns/behavioral/iterator.py
, patterns/behavioral/iterator_alt.py
The Chain of Responsibility pattern is implemented in …/chain_of_responsibility.py
. It defines an abstract Handler
class and concrete handlers that process requests within specific ranges. Key points:
State Management Patterns[Edit section][Copy link]
References: patterns/behavioral/memento.py
, patterns/behavioral/state.py
The Memento and State patterns are implemented to manage object state changes and persistence.
Read moreAlgorithmic Patterns[Edit section][Copy link]
References: patterns/behavioral/strategy.py
, patterns/behavioral/template.py
The Strategy pattern is implemented in …/strategy.py
, allowing dynamic selection of discount algorithms for order pricing. Key components:
Abstract Factory[Edit section][Copy link]
References: patterns/creational/abstract_factory.py
The Abstract Factory pattern is implemented using a Pet
abstract base class with speak()
and __str__()
abstract methods. Concrete implementations Dog
and Cat
provide specific behaviors for these methods.
Borg (Monostate)[Edit section][Copy link]
References: patterns/creational/borg.py
The Borg
class implements the Borg (Monostate) pattern, which shares state between instances rather than sharing instance identity. This is achieved through a class attribute _shared_state
, a dictionary that stores the shared state. The __init__
method sets the instance's __dict__
to _shared_state
, ensuring all instances share the same attribute dictionary.
Builder[Edit section][Copy link]
References: patterns/creational/builder.py
The Builder pattern is implemented in …/builder.py
using two distinct approaches:
Factory[Edit section][Copy link]
References: patterns/creational/factory.py
The Factory pattern is implemented using a get_localizer()
function that creates and returns Localizer
objects based on the requested language. Key components include:
Lazy Evaluation[Edit section][Copy link]
References: patterns/creational/lazy_evaluation.py
The Lazy Evaluation pattern is implemented using two approaches: the lazy_property
class and the lazy_property2
function decorator.
Object Pool[Edit section][Copy link]
References: patterns/creational/pool.py
The ObjectPool
class manages a pool of reusable objects, optimizing resource allocation and improving performance. Key features include:
Prototype[Edit section][Copy link]
References: patterns/creational/prototype.py
The Prototype
class implements the Prototype pattern, allowing objects to be created by cloning a prototypical instance. This pattern is useful when object creation is expensive or when there are only a few different combinations of object state.
3-Tier Architecture[Edit section][Copy link]
References: patterns/structural/3-tier.py
The three-tier architecture pattern is implemented using three main classes:
Read moreAdapter Pattern[Edit section][Copy link]
References: patterns/structural/adapter.py
The Adapter
class in …/adapter.py
allows objects with incompatible interfaces to work together. It wraps an object and provides a new interface for it, enabling seamless integration of different classes.
Bridge Pattern[Edit section][Copy link]
References: patterns/structural/bridge.py
The Bridge pattern separates the abstraction (CircleShape
) from its implementation (DrawingAPI1
and DrawingAPI2
). This design allows for independent variation of both the shape and drawing implementations.
Composite Pattern[Edit section][Copy link]
References: patterns/structural/composite.py
The Composite pattern is implemented using three main classes: Graphic
, CompositeGraphic
, and Ellipse
.
Decorator Pattern[Edit section][Copy link]
References: patterns/structural/decorator.py
The Decorator pattern is implemented using a base TextTag
class and decorator classes BoldWrapper
and ItalicWrapper
. This pattern allows for dynamic addition of HTML tags to text objects.
Facade Pattern[Edit section][Copy link]
References: patterns/structural/facade.py
The ComputerFacade
class simplifies the interaction with complex computer components (CPU
, Memory
, and SolidStateDrive
). It provides a single start()
method that coordinates the startup process:
Flyweight Pattern[Edit section][Copy link]
References: patterns/structural/flyweight.py
, patterns/structural/flyweight_with_metaclass.py
The Flyweight pattern is implemented in two different ways:
Read moreFront Controller Pattern[Edit section][Copy link]
References: patterns/structural/front_controller.py
The Front Controller pattern centralizes request handling in web applications through the RequestController
class. This class acts as the entry point for all incoming requests, dispatching them to the appropriate handlers:
MVC Pattern[Edit section][Copy link]
References: patterns/structural/mvc.py
The Model-View-Controller (MVC) pattern is implemented using three main components:
Read moreProxy Pattern[Edit section][Copy link]
References: patterns/structural/proxy.py
The Proxy pattern is implemented using three main classes: Subject
, RealSubject
, and Proxy
.
Hierarchical State Machine (HSM)[Edit section][Copy link]
References: patterns/other/hsm
The HierachicalStateMachine
class manages unit states and transitions in a Hierarchical State Machine (HSM) implementation. It initializes states (Active
, Standby
, Suspect
, and Failed
) and handles incoming messages, transitioning the unit to appropriate states based on message types.
Blackboard Pattern[Edit section][Copy link]
References: patterns/other/blackboard.py
The Blackboard pattern is implemented through several key components:
Read moreGraph Search Algorithms[Edit section][Copy link]
References: patterns/other/graph_search.py
The GraphSearch
class implements Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for graph traversal and path finding. The graph is represented as a dictionary where keys are nodes and values are lists of neighboring nodes.
Creational Patterns[Edit section][Copy link]
References: patterns/creational
The …/creational
directory contains implementations of various creational design patterns:
Structural Patterns[Edit section][Copy link]
References: patterns/structural
The …/structural
directory contains implementations of various structural design patterns:
Other Design Patterns[Edit section][Copy link]
References: patterns/other
The …/other
directory contains implementations of design patterns and algorithms that don't fit into the traditional behavioral, creational, or structural categories:
Fundamental Patterns[Edit section][Copy link]
References: patterns/fundamental
The Delegation Pattern is implemented in …/delegation_pattern.py
. This pattern enables object composition for code reuse, similar to inheritance. Key components: