faif/python-patterns

 faif / python-patterns

faif / python-patterns

A collection of design patterns/idioms in Python

python-patterns113701137011380113801139011390114001140011410114101142011420114301143011440114401145011450114601146011470114701148011480114901149011500115002017-05-132017-05-142017-05-152017-05-162017-05-172017-05-182017-05-192017-05-202017-05-212017-05-222017-05-232017-05-242017-05-252017-05-262017-05-27python-patterns113668.23112389909199.565737722017-05-131137237.6279949673191.240091592017-05-141139067.0248660355166.2631532022017-05-151140796.4217371037142.6738225032017-05-1611419125.818608172126.0225302442017-05-1711431155.21547924109.3712379852017-05-1811442184.61235030894.1075534152017-05-1911451214.00922137681.61908422112017-05-2011457243.40609244573.29343809172017-05-2111466272.80296351360.80496889782017-05-2211470302.19983458155.25453814492017-05-2311485331.59670564934.44042282162017-05-2411494360.99357671721.95195362762017-05-2511501390.39044778612.23869981012017-05-2611507419.7873188543.913053680782017-05-27star

 README

python-patterns

A collection of design patterns and idioms in Python.

When an implementation is added or modified, be sure to update this file and rerun append_output.sh (eg. ./append_output.sh borg.py) to keep the output comments at the bottom up to date.

Current Patterns:

Creational Patterns:

PatternDescription
abstract_factoryuse a generic function with specific factories
borga singleton with shared-state among instances
builderinstead of using multiple constructors, builder object receives parameters and returns constructed objects
factory_methoddelegate a specialized function/method to create instances
lazy_evaluationlazily-evaluated property pattern in Python
poolpreinstantiate and maintain a group of instances of the same type
prototypeuse a factory and clones of a prototype for new instances (if instantiation is expensive)

Structural Patterns:

PatternDescription
3-tierdata<->business logic<->presentation separation (strict relationships)
adapteradapt one interface to another using a white-list
bridgea client-provider middleman to soften interface changes
compositeencapsulate and provide access to a number of different objects
decoratorwrap functionality with other functionality in order to affect outputs
facadeuse one class as an API to a number of others
flyweighttransparently reuse existing instances of objects with similar/identical state
front_controllersingle handler requests coming to the application
mvcmodel<->view<->controller (non-strict relationships)
proxyan object funnels operations to something else

Behavioral Patterns:

PatternDescription
chainapply a chain of successive handlers to try and process the data
cataloggeneral methods will call different specialized methods based on construction parameter
chaining_methodcontinue callback next object method
commandbundle a command and arguments to call later
iteratortraverse a container and access the container's elements
mediatoran object that knows how to connect other objects and act as a proxy
mementogenerate an opaque token that can be used to go back to a previous state
observerprovide a callback for notification of events/changes to data
publish_subscribea source syndicates events/data to 0+ registered listeners
registrykeep track of all subclasses of a given class
specificationbusiness rules can be recombined by chaining the business rules together using boolean logic
statelogic is organized into a discrete number of potential states and the next state that can be transitioned to
strategyselectable operations over the same data
templatean object imposes a structure but takes pluggable components
visitorinvoke a callback for all items of a collection

Fundamental Patterns:

PatternDescription
delegation_patternan object handles a request by delegating to a second object (the delegate)

Others:

PatternDescription
blackboardarchitectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern
graph_searchgraphing algorithms - non gang of four pattern
hsmhierarchical state machine - non gang of four pattern