faif/python-patterns

 faif / python-patterns

faif / python-patterns

A collection of design patterns/idioms in Python

python-patterns-2-2002244668810101212141416161818202022222017-06-222017-06-232017-06-242017-06-252017-06-262017-06-272017-06-282017-06-292017-06-302017-07-012017-07-022017-07-032017-07-042017-07-052017-07-062017-07-072017-07-082017-07-092017-07-102017-07-112017-07-122017-07-132017-07-142017-07-152017-07-162017-07-172017-07-182017-07-192017-07-202017-07-21python-patterns79.11538461538102.7663550892017-06-221224.831564986771.63744604672017-06-23940.547745358190.3147914722017-06-24356.2639257294127.6694823232017-06-251171.980106100877.86322785522017-06-261487.696286472159.18588242982017-06-278103.41246684496.54057328052017-06-2820119.12864721521.83119157922017-06-2914134.84482758659.18588242982017-06-306150.561007958108.9921368972017-07-017166.277188329102.7663550892017-07-0213181.993368765.41166423832017-07-038197.70954907296.54057328052017-07-048213.42572944396.54057328052017-07-055229.141909814115.2179187062017-07-064244.858090186121.4437005142017-07-073260.574270557127.6694823232017-07-0811276.29045092877.86322785522017-07-0911292.006631377.86322785522017-07-107307.722811671102.7663550892017-07-118323.43899204296.54057328052017-07-1210339.15517241484.08900966362017-07-138354.87135278596.54057328052017-07-148370.58753315696.54057328052017-07-156386.303713528108.9921368972017-07-1617402.01989389940.50853700452017-07-1711417.73607427177.86322785522017-07-1814433.45225464259.18588242982017-07-1915449.16843501352.96010062142017-07-2023464.8846153853.153846153852017-07-2109.11538461538146.3468277482017-06-22624.8315649867108.9921368972017-06-23140.5477453581140.121045942017-06-24056.2639257294146.3468277482017-06-25271.9801061008133.8952641312017-06-26487.6962864721121.4437005142017-06-272103.412466844133.8952641312017-06-282119.128647215133.8952641312017-06-290134.844827586146.3468277482017-06-303150.561007958127.6694823232017-07-012166.277188329133.8952641312017-07-023181.9933687127.6694823232017-07-031197.709549072140.121045942017-07-041213.425729443140.121045942017-07-050229.141909814146.3468277482017-07-064244.858090186121.4437005142017-07-072260.574270557133.8952641312017-07-080276.290450928146.3468277482017-07-090292.0066313146.3468277482017-07-100307.722811671146.3468277482017-07-112323.438992042133.8952641312017-07-126339.155172414108.9921368972017-07-131354.871352785140.121045942017-07-143370.587533156127.6694823232017-07-151386.303713528140.121045942017-07-16-1402.019893899152.5726095562017-07-174417.736074271121.4437005142017-07-187433.452254642102.7663550892017-07-195449.168435013115.2179187062017-07-203464.884615385127.6694823232017-07-2119.11538461538140.121045942017-06-22124.8315649867140.121045942017-06-23-240.5477453581158.7983913652017-06-24-156.2639257294152.5726095562017-06-25171.9801061008140.121045942017-06-26087.6962864721146.3468277482017-06-27-2103.412466844158.7983913652017-06-284119.128647215121.4437005142017-06-291134.844827586140.121045942017-06-302150.561007958133.8952641312017-07-010166.277188329146.3468277482017-07-021181.9933687140.121045942017-07-030197.709549072146.3468277482017-07-040213.425729443146.3468277482017-07-051229.141909814140.121045942017-07-060244.858090186146.3468277482017-07-07-1260.574270557152.5726095562017-07-080276.290450928146.3468277482017-07-091292.0066313140.121045942017-07-100307.722811671146.3468277482017-07-111323.438992042140.121045942017-07-122339.155172414133.8952641312017-07-130354.871352785146.3468277482017-07-140370.587533156146.3468277482017-07-151386.303713528140.121045942017-07-160402.019893899146.3468277482017-07-170417.736074271146.3468277482017-07-18-1433.452254642152.5726095562017-07-190449.168435013146.3468277482017-07-203464.884615385127.6694823232017-07-21starforkwatch

 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