faif /python-patterns

A collection of design patterns/idioms in Python

★ 43,093⑂ 6,962👁 1,567⑃ 402● 97▤ 0◈ 0
PythonCreated 2012-06-06
43,093
stars
+1
6,962
forks
1,567
watchers
402
prs
97
issues
0
releases
0
discussions

README

python-patterns

A collection of design patterns and idioms in Python.

Remember that each pattern has its own trade-offs. And you need to pay attention more to why you're choosing a certain pattern than to how to implement it.

 Creational Patterns

 Patterns that deal with object creation — abstracting and controlling how instances are made.

mermaid
graph LR
    Client --|requests object| AbstractFactory
    AbstractFactory --|delegates to| ConcreteFactory
    ConcreteFactory --|produces| Product

    Builder --|step-by-step| Director
    Director --|returns| BuiltObject

    FactoryMethod --|subclass decides| ConcreteProduct
    Pool --|reuses| PooledInstance


| Pattern | Description |
|:-------:| ----------- |
| abstract_factory | use a generic function with specific factories |
| borg | a singleton with shared-state among instances |
| builder | instead of using multiple constructors, builder object receives parameters and returns constructed objects |
| factory | delegate a specialized function/method to create instances |
| lazy_evaluation | 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 (if instantiation is expensive) |

 Structural Patterns

 Patterns that define how classes and objects are composed to form larger, flexible structures.

mermaid
graph TD
    Client -- Facade
    Facade -- SubsystemA
    Facade -- SubsystemB
    Facade -- SubsystemC

    Client2 -- Adapter
    Adapter -- LegacyService

    Client3 -- Proxy
    Proxy --|controls access to| RealSubject

    Component -- Composite
    Composite -- Leaf1
    Composite -- Leaf2


|                           Pattern                           | Description                                                                    |
|:-----------------------------------------------------------:|--------------------------------------------------------------------------------|
|           3-tier           | databusiness logicpresentation separation (strict relationships)         |
|          adapter          | adapt one interface to another using a white-list                              |
|           bridge           | a client-provider middleman to soften interface changes                        |
|        composite        | lets clients treat individual objects and compositions uniformly               |
|        decorator        | wrap functionality with other functionality in order 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 requests coming to the application                              |
|              mvc              | modelviewcontroller (non-strict relationships)                           |
|     

Top issues

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON