repo logo
python-patterns
Language
Python
Created
06/06/2012
Last updated
09/06/2024
autowiki
Software Version
u-0.0.1Basic
Generated from
Commit
328b2d
Generated on
09/07/2024

python-patterns
[Edit section]
[Copy link]

• • •
Architecture Diagram for python-patterns
Architecture Diagram for python-patterns

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 and HexViewer 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

• • •
Architecture Diagram for Behavioral Patterns
Architecture Diagram for Behavioral Patterns

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.

Read more

Design Pattern Implementations
[Edit section]
[Copy link]

References: patterns/behavioral

• • •
Architecture Diagram for Design Pattern Implementations
Architecture Diagram for Design Pattern Implementations

The Behavioral Patterns directory (…/behavioral) contains implementations of various design patterns focusing on object interactions and responsibilities.

Read more

Communication and Interaction Patterns
[Edit section]
[Copy link]

References: patterns/behavioral/mediator.py, patterns/behavioral/observer.py, patterns/behavioral/publish_subscribe.py

• • •
Architecture Diagram for Communication and Interaction Patterns
Architecture Diagram for Communication and Interaction Patterns

The Mediator, Observer, and Publish-Subscribe patterns are implemented to manage communication between objects.

Read more

Control 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

• • •
Architecture Diagram for Control Flow Patterns
Architecture Diagram for Control Flow Patterns

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:

Read more

State Management Patterns
[Edit section]
[Copy link]

References: patterns/behavioral/memento.py, patterns/behavioral/state.py

• • •
Architecture Diagram for State Management Patterns
Architecture Diagram for State Management Patterns

The Memento and State patterns are implemented to manage object state changes and persistence.

Read more

Algorithmic Patterns
[Edit section]
[Copy link]

References: patterns/behavioral/strategy.py, patterns/behavioral/template.py

• • •
Architecture Diagram for Algorithmic Patterns
Architecture Diagram for Algorithmic Patterns

The Strategy pattern is implemented in …/strategy.py, allowing dynamic selection of discount algorithms for order pricing. Key components:

Read more

Abstract Factory
[Edit section]
[Copy link]

References: patterns/creational/abstract_factory.py

• • •
Architecture Diagram for Abstract Factory
Architecture Diagram for Abstract Factory

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.

Read more

Borg (Monostate)
[Edit section]
[Copy link]

References: patterns/creational/borg.py

• • •
Architecture Diagram for Borg (Monostate)
Architecture Diagram for Borg (Monostate)

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.

Read more

Builder
[Edit section]
[Copy link]

References: patterns/creational/builder.py

• • •
Architecture Diagram for Builder
Architecture Diagram for Builder

The Builder pattern is implemented in …/builder.py using two distinct approaches:

Read more

Factory
[Edit section]
[Copy link]

References: patterns/creational/factory.py

• • •
Architecture Diagram for Factory
Architecture Diagram for Factory

The Factory pattern is implemented using a get_localizer() function that creates and returns Localizer objects based on the requested language. Key components include:

Read more

Lazy Evaluation
[Edit section]
[Copy link]

References: patterns/creational/lazy_evaluation.py

• • •
Architecture Diagram for Lazy Evaluation
Architecture Diagram for Lazy Evaluation

The Lazy Evaluation pattern is implemented using two approaches: the lazy_property class and the lazy_property2 function decorator.

Read more

Object Pool
[Edit section]
[Copy link]

References: patterns/creational/pool.py

• • •
Architecture Diagram for Object Pool
Architecture Diagram for Object Pool

The ObjectPool class manages a pool of reusable objects, optimizing resource allocation and improving performance. Key features include:

Read more

Prototype
[Edit section]
[Copy link]

References: patterns/creational/prototype.py

• • •
Architecture Diagram for Prototype
Architecture Diagram for Prototype

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.

Read more

3-Tier Architecture
[Edit section]
[Copy link]

References: patterns/structural/3-tier.py

• • •
Architecture Diagram for 3-Tier Architecture
Architecture Diagram for 3-Tier Architecture

The three-tier architecture pattern is implemented using three main classes:

Read more

Adapter Pattern
[Edit section]
[Copy link]

References: patterns/structural/adapter.py

• • •
Architecture Diagram for Adapter Pattern
Architecture Diagram for Adapter Pattern

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.

Read more

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.

Read more

Composite Pattern
[Edit section]
[Copy link]

References: patterns/structural/composite.py

• • •
Architecture Diagram for Composite Pattern
Architecture Diagram for Composite Pattern

The Composite pattern is implemented using three main classes: Graphic, CompositeGraphic, and Ellipse.

Read more

Decorator Pattern
[Edit section]
[Copy link]

References: patterns/structural/decorator.py

• • •
Architecture Diagram for Decorator Pattern
Architecture Diagram for Decorator Pattern

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.

Read more

Facade Pattern
[Edit section]
[Copy link]

References: patterns/structural/facade.py

• • •
Architecture Diagram for Facade Pattern
Architecture Diagram for Facade Pattern

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:

Read more

Flyweight Pattern
[Edit section]
[Copy link]

References: patterns/structural/flyweight.py, patterns/structural/flyweight_with_metaclass.py

• • •
Architecture Diagram for Flyweight Pattern
Architecture Diagram for Flyweight Pattern

The Flyweight pattern is implemented in two different ways:

Read more

Front Controller Pattern
[Edit section]
[Copy link]

References: patterns/structural/front_controller.py

• • •
Architecture Diagram for Front Controller Pattern
Architecture Diagram for Front Controller Pattern

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:

Read more

MVC Pattern
[Edit section]
[Copy link]

References: patterns/structural/mvc.py

• • •
Architecture Diagram for MVC Pattern
Architecture Diagram for MVC Pattern

The Model-View-Controller (MVC) pattern is implemented using three main components:

Read more

Proxy Pattern
[Edit section]
[Copy link]

References: patterns/structural/proxy.py

• • •
Architecture Diagram for Proxy Pattern
Architecture Diagram for Proxy Pattern

The Proxy pattern is implemented using three main classes: Subject, RealSubject, and Proxy.

Read more

Hierarchical State Machine (HSM)
[Edit section]
[Copy link]

References: patterns/other/hsm

• • •
Architecture Diagram for Hierarchical State Machine (HSM)
Architecture Diagram for Hierarchical State Machine (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.

Read more

Blackboard Pattern
[Edit section]
[Copy link]

References: patterns/other/blackboard.py

• • •
Architecture Diagram for Blackboard Pattern
Architecture Diagram for Blackboard Pattern

The Blackboard pattern is implemented through several key components:

Read more

Graph Search Algorithms
[Edit section]
[Copy link]

References: patterns/other/graph_search.py

• • •
Architecture Diagram for Graph Search Algorithms
Architecture Diagram for Graph Search Algorithms

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.

Read more

Creational Patterns
[Edit section]
[Copy link]

References: patterns/creational

• • •
Architecture Diagram for Creational Patterns
Architecture Diagram for Creational Patterns

The …/creational directory contains implementations of various creational design patterns:

Read more

Structural Patterns
[Edit section]
[Copy link]

References: patterns/structural

The …/structural directory contains implementations of various structural design patterns:

Read more

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:

Read more

Fundamental Patterns
[Edit section]
[Copy link]

References: patterns/fundamental

• • •
Architecture Diagram for Fundamental Patterns
Architecture Diagram for Fundamental Patterns

The Delegation Pattern is implemented in …/delegation_pattern.py. This pattern enables object composition for code reuse, similar to inheritance. Key components:

Read more