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
#143Needs a COPYRIGHT/LICENSE file18 comments · 2016-05-17#244[Question] Functional Programming Patterns9 comments · 2018-09-29#333[Suggestion] Notebook version of the repo6 comments · 2020-07-01#373Support for type hints5 comments · 2021-01-24#277[Infrastructure] Code coverage4 comments · 2019-02-07#245Add Automation testing pattern4 comments · 2018-10-04#278Add Data Access Object (DAO), Data Transfer Object (DTO) patterns?3 comments · 2019-02-07#1203-tier.py and mvc.py are almost identical3 comments · 2016-02-20