python-patterns

repository·master·Indexed 12 days ago

https://github.com/faif/python-patterns

A collection of design patterns and Pythonic idioms version 0.1.0, providing implementations and explanations for Creational, Structural, and Behavioral patterns. The library covers patterns such as abstract_factory, adapter, and observer, while providing guidance on avoiding anti-patterns like the Singleton and God Object.

Tokens
4.2K
Snippets
3
Records
5
Agent score
49%

What's inside python-patterns

  1. Overview of python-patterns

    master
    The python-patterns repository is a collection of design patterns and Pythonic idioms. It categorizes patterns into several groups: Creational, Structural, Behavioral, Design for Testability, Fundamental, and others. The project emphasizes understanding the trade-offs and the 'why' behind choosing a pattern rather than just the implementation details.
  2. Avoid these Anti-Patterns in Python

    master

    The following patterns are generally discouraged in Python development:

    • Singleton: Python modules are already singletons (imported only once). Explicit singleton classes add unnecessary complexity. Use module-level variables or dependency injection instead.
    • God Object: Avoid centralizing too much logic in a single class, as it makes code harder to test and maintain. Split functionality into smaller, cohesive classes.
    • Inheritance overuse: Deep inheritance trees make code brittle. Favor composition and delegation over inheritance.
  3. Creational Patterns Reference

    master

    Creational patterns deal with object creation, abstracting and controlling how instances are made. Available patterns include:

    • abstract_factory: Use a generic function with specific factories.
    • borg: A singleton with shared-state among instances.
    • builder: Use a builder object to receive parameters and return constructed objects instead of multiple constructors.
    • factory: Delegate a specialized function/method to create instances.
    • lazy_evaluation: A lazily-evaluated property pattern in Python.
    • pool: Preinstantiate and maintain a group of instances of the same type.
    • prototype: Use a factory and clones of a prototype for new instances (useful if instantiation is expensive).
    | Pattern | Description |
    |:-------:| ----------- |
    | [abstract_factory](patterns/creational/abstract_factory.py) | use a generic function with specific factories |
    | [borg](patterns/creational/borg.py) | a singleton with shared-state among instances |
    | [builder](patterns/creational/builder.py) | instead of using multiple constructors, builder object receives parameters and returns constructed objects |
    | [factory](patterns/creational/factory.py) | delegate a specialized function/method to create instances |
    | [lazy_evaluation](patterns/creational/lazy_evaluation.py) | lazily-evaluated property pattern in Python |
    | [pool](patterns/creational/pool.py) | preinstantiate and maintain a group of instances of the same type |
    | [prototype](patterns/creational/prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) |
  4. Behavioral Patterns Reference

    master

    Behavioral patterns are concerned with communication and responsibility between objects. Available patterns include:

    • chain_of_responsibility: Apply a chain of successive handlers to process data.
    • catalog: General methods call specialized methods based on construction parameters.
    • chaining_method: Continue callback to the next object method.
    • command: Bundle a command and arguments to call later.
    • interpreter: Define a grammar for a language to interpret statements.
    • iterator: Traverse a container and access its elements.
    • mediator: An object that connects other objects and acts as a proxy.
    • memento: Generate an opaque token to return to a previous state.
    • observer: Provide a callback for notification of events/changes to data.
    • publish_subscribe: A source syndicates events/data to 0+ registered listeners.
    • registry: Keep track of all subclasses of a given class.
    • servant: Provide common functionality to a group of classes without using inheritance.
    • specification: Recombine business rules by chaining them using boolean logic.
    • state: Organize logic into discrete states with defined transitions.
    • strategy: Selectable operations over the same data.
    • template: An object imposes a structure but takes pluggable components.
    • visitor: Invoke a callback for all items of a collection.
    |                                  Pattern                                  | Description                                                                                                  |
    |:-------------------------------------------------------------------------:|--------------------------------------------------------------------------------------------------------------|
    | [chain_of_responsibility](patterns/behavioral/chain_of_responsibility.py) | apply a chain of successive handlers to try and process the data                                             |
    |                 [catalog](patterns/behavioral/catalog.py)                 | general methods will call different specialized methods based on construction parameter                      |
    |         [chaining_method](patterns/behavioral/chaining_method.py)         | continue callback next object method                                                                         |
    |                 [command](patterns/behavioral/command.py)                 | bundle a command and arguments to call later                                                                 |
    |                [interpreter](patterns/behavioral/interpreter.py)            | define a grammar for a language and use it to interpret statements                                             |
    |                [iterator](patterns/behavioral/iterator.py)                | traverse a container and access the container's elements                                                     |
    |       [iterator](patterns/behavioral/iterator_alt.py) (alt. impl.)                                                                                                                                                                 |
    |                [mediator](patterns/behavioral/mediator.py)                 | an object that knows how to connect other objects and act as a proxy                                        |
    |                 [memento](patterns/behavioral/memento.py)                 | generate an opaque token that can be used to go back to a previous state                                    |
    |                 [observer](patterns/behavioral/observer.py)               | provide a callback for notification of events/changes to data                                                |
    |       [publish_subscribe](patterns/behavioral/publish_subscribe.py)       | a source syndicates events/data to 0+ registered listeners                                                                  |
    |                [registry](patterns/behavioral/registry.py)                 | keep track of all subclasses of a given class                                                               |$
    |                 [servant](patterns/behavioral/servant.py)                 | provide common functionality to a group of classes without using inheritance                                |$
    |           [specification](patterns/behavioral/specification.py)           | business rules can be recombined by chaining the business rules together using boolean logic                 |$
    |                   [state](patterns/behavioral/state.py)                   | logic is organized into a discrete number of potential states and the next state that can be transitioned to |$
    |                   [strategy](patterns/behavioral/strategy.py)               | selectable operations over the same data                                                                     |
    |                   [template](patterns/behavioral/template.py)               | an object imposes a structure but takes pluggable components                                                               |$
    |                   [visitor](patterns/behavioral/visitor.py)               | invoke a callback for all items of a collection                                                               |
  5. Structural Patterns Reference

    master

    Structural patterns define how classes and objects are composed to form larger, flexible structures. Available patterns include:

    • 3-tier: Separation of data, business logic, and presentation (strict relationships).
    • adapter: Adapt one interface to another using a white-list.
    • bridge: A client-provider middleman to soften interface changes.
    • composite: Treat individual objects and compositions uniformly.
    • decorator: Wrap functionality with other functionality to affect outputs.
    • facade: Use one class as an API to a number of others.
    • flyweight: Transparently reuse existing instances of objects with similar/identical state.
    • front_controller: Single handler for requests coming to the application.
    • mvc: Model-View-Controller (non-strict relationships).
    • proxy: An object that funnels operations to something else.
    |                           Pattern                           | Description                                                                    |
    |:-----------------------------------------------------------:|--------------------------------------------------------------------------------|
    |           [3-tier](patterns/structural/3-tier.py)           | data<->business logic<->presentation separation (strict relationships) |
    |          [adapter](patterns/structural/adapter.py)          | adapt one interface to another using a white-list |
    |         [bridge](patterns/structural/bridge.py)          | a client-provider middleman to soften interface changes |
    |          [composite](patterns/structural/composite.py)          | lets clients treat individual objects and compositions uniformly               |
    |          [decorator](patterns/structural/decorator.py)          | wrap functionality with other functionality in order to affect outputs         |
    |           [facade](patterns/structural/facade.py)          | use one class as an API to a number of others                                |
    |        [flyweight](patterns/structural/flyweight.py)          | transparently reuse existing instances of objects with similar/identical state |
    | [front_controller](patterns/structural/front_controller.py) | single handler requests coming to the application                              |
    |              [mvc](patterns/structural/mvc.py)              | model<->view<->controller (non-strict relationships) |
    |            [proxy](patterns/structural/proxy.py)            | an object funnels operations to something else |