Skip to content

Commit c41ae4d

Browse files
authored
Merge branch 'faif:master' into master
2 parents 5dbd206 + 74151cf commit c41ae4d

1 file changed

Lines changed: 91 additions & 56 deletions

File tree

README.md

Lines changed: 91 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
python-patterns
2-
===============
1+
# python-patterns
32

43
A collection of design patterns and idioms in Python.
54

65
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.
76

8-
Current Patterns
9-
----------------
7+
## Creational Patterns
108

11-
__Creational Patterns__:
9+
> Patterns that deal with **object creation** — abstracting and controlling how instances are made.
10+
11+
```mermaid
12+
graph LR
13+
Client -->|requests object| AbstractFactory
14+
AbstractFactory -->|delegates to| ConcreteFactory
15+
ConcreteFactory -->|produces| Product
16+
17+
Builder -->|step-by-step| Director
18+
Director -->|returns| BuiltObject
19+
20+
FactoryMethod -->|subclass decides| ConcreteProduct
21+
Pool -->|reuses| PooledInstance
22+
```
1223

1324
| Pattern | Description |
1425
|:-------:| ----------- |
@@ -20,7 +31,27 @@ __Creational Patterns__:
2031
| [pool](patterns/creational/pool.py) | preinstantiate and maintain a group of instances of the same type |
2132
| [prototype](patterns/creational/prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) |
2233

23-
__Structural Patterns__:
34+
## Structural Patterns
35+
36+
> Patterns that define **how classes and objects are composed** to form larger, flexible structures.
37+
38+
```mermaid
39+
graph TD
40+
Client --> Facade
41+
Facade --> SubsystemA
42+
Facade --> SubsystemB
43+
Facade --> SubsystemC
44+
45+
Client2 --> Adapter
46+
Adapter --> LegacyService
47+
48+
Client3 --> Proxy
49+
Proxy -->|controls access to| RealSubject
50+
51+
Component --> Composite
52+
Composite --> Leaf1
53+
Composite --> Leaf2
54+
```
2455

2556
| Pattern | Description |
2657
|:-------:| ----------- |
@@ -35,14 +66,33 @@ __Structural Patterns__:
3566
| [mvc](patterns/structural/mvc.py) | model<->view<->controller (non-strict relationships) |
3667
| [proxy](patterns/structural/proxy.py) | an object funnels operations to something else |
3768

38-
__Behavioral Patterns__:
69+
## Behavioral Patterns
70+
71+
> Patterns concerned with **communication and responsibility** between objects.
72+
73+
```mermaid
74+
graph LR
75+
Sender -->|sends event| Observer1
76+
Sender -->|sends event| Observer2
77+
78+
Request --> Handler1
79+
Handler1 -->|passes if unhandled| Handler2
80+
Handler2 -->|passes if unhandled| Handler3
81+
82+
Context -->|delegates to| Strategy
83+
Strategy -->|executes| Algorithm
84+
85+
Originator -->|saves state to| Memento
86+
Caretaker -->|holds| Memento
87+
```
3988

4089
| Pattern | Description |
4190
|:-------:| ----------- |
4291
| [chain_of_responsibility](patterns/behavioral/chain_of_responsibility.py) | apply a chain of successive handlers to try and process the data |
4392
| [catalog](patterns/behavioral/catalog.py) | general methods will call different specialized methods based on construction parameter |
4493
| [chaining_method](patterns/behavioral/chaining_method.py) | continue callback next object method |
4594
| [command](patterns/behavioral/command.py) | bundle a command and arguments to call later |
95+
| [interpreter](patterns/behavioral/interpreter.py) | define a grammar for a language and use it to interpret statements |
4696
| [iterator](patterns/behavioral/iterator.py) | traverse a container and access the container's elements |
4797
| [iterator](patterns/behavioral/iterator_alt.py) (alt. impl.)| traverse a container and access the container's elements |
4898
| [mediator](patterns/behavioral/mediator.py) | an object that knows how to connect other objects and act as a proxy |
@@ -51,77 +101,32 @@ __Behavioral Patterns__:
51101
| [publish_subscribe](patterns/behavioral/publish_subscribe.py) | a source syndicates events/data to 0+ registered listeners |
52102
| [registry](patterns/behavioral/registry.py) | keep track of all subclasses of a given class |
53103
| [servant](patterns/behavioral/servant.py) | provide common functionality to a group of classes without using inheritance |
54-
| [specification](patterns/behavioral/specification.py) | business rules can be recombined by chaining the business rules together using boolean logic |
104+
| [specification](patterns/behavioral/specification.py) | business rules can be recombined by chaining the business rules together using boolean logic |
55105
| [state](patterns/behavioral/state.py) | logic is organized into a discrete number of potential states and the next state that can be transitioned to |
56106
| [strategy](patterns/behavioral/strategy.py) | selectable operations over the same data |
57107
| [template](patterns/behavioral/template.py) | an object imposes a structure but takes pluggable components |
58108
| [visitor](patterns/behavioral/visitor.py) | invoke a callback for all items of a collection |
59109

60-
__Design for Testability Patterns__:
110+
## Design for Testability Patterns
61111

62112
| Pattern | Description |
63113
|:-------:| ----------- |
64114
| [dependency_injection](patterns/dependency_injection.py) | 3 variants of dependency injection |
65115

66-
__Fundamental Patterns__:
116+
## Fundamental Patterns
67117

68118
| Pattern | Description |
69119
|:-------:| ----------- |
70120
| [delegation_pattern](patterns/fundamental/delegation_pattern.py) | an object handles a request by delegating to a second object (the delegate) |
71121

72-
__Others__:
122+
## Others
73123

74124
| Pattern | Description |
75125
|:-------:| ----------- |
76126
| [blackboard](patterns/other/blackboard.py) | architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern |
77127
| [graph_search](patterns/other/graph_search.py) | graphing algorithms - non gang of four pattern |
78128
| [hsm](patterns/other/hsm/hsm.py) | hierarchical state machine - non gang of four pattern |
79129

80-
81-
Videos
82-
------
83-
[Design Patterns in Python by Peter Ullrich](https://www.youtube.com/watch?v=bsyjSW46TDg)
84-
85-
[Sebastian Buczyński - Why you don't need design patterns in Python?](https://www.youtube.com/watch?v=G5OeYHCJuv0)
86-
87-
[You Don't Need That!](https://www.youtube.com/watch?v=imW-trt0i9I)
88-
89-
[Pluggable Libs Through Design Patterns](https://www.youtube.com/watch?v=PfgEU3W0kyU)
90-
91-
92-
Contributing
93-
------------
94-
When an implementation is added or modified, please review the following guidelines:
95-
96-
##### Docstrings
97-
Add module level description in form of a docstring with links to corresponding references or other useful information.
98-
99-
Add "Examples in Python ecosystem" section if you know some. It shows how patterns could be applied to real-world problems.
100-
101-
[facade.py](patterns/structural/facade.py) has a good example of detailed description,
102-
but sometimes the shorter one as in [template.py](patterns/behavioral/template.py) would suffice.
103-
104-
##### Python 2 compatibility
105-
To see Python 2 compatible versions of some patterns please check-out the [legacy](https://github.com/faif/python-patterns/tree/legacy) tag.
106-
107-
##### Update README
108-
When everything else is done - update corresponding part of README.
109-
110-
##### Travis CI
111-
Please run the following before submitting a patch
112-
- `black .` This lints your code.
113-
114-
Then either:
115-
- `tox` or `tox -e ci37` This runs unit tests. see tox.ini for further details.
116-
- If you have a bash compatible shell use `./lint.sh` This script will lint and test your code. This script mirrors the CI pipeline actions.
117-
118-
You can also run `flake8` or `pytest` commands manually. Examples can be found in `tox.ini`.
119-
120-
## Contributing via issue triage [![Open Source Helpers](https://www.codetriage.com/faif/python-patterns/badges/users.svg)](https://www.codetriage.com/faif/python-patterns)
121-
122-
You can triage issues and pull requests which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to [subscribe to python-patterns on CodeTriage](https://www.codetriage.com/faif/python-patterns).
123-
124-
125130
## 🚫 Anti-Patterns
126131

127132
This section lists some common design patterns that are **not recommended** in Python and explains why.
@@ -144,3 +149,33 @@ This section lists some common design patterns that are **not recommended** in P
144149
- Prefer composition and delegation.
145150
- “Favor composition over inheritance.”
146151

152+
## Videos
153+
154+
* [Design Patterns in Python by Peter Ullrich](https://www.youtube.com/watch?v=bsyjSW46TDg)
155+
* [Sebastian Buczyński - Why you don't need design patterns in Python?](https://www.youtube.com/watch?v=G5OeYHCJuv0)
156+
* [You Don't Need That!](https://www.youtube.com/watch?v=imW-trt0i9I)
157+
* [Pluggable Libs Through Design Patterns](https://www.youtube.com/watch?v=PfgEU3W0kyU)
158+
159+
## Contributing
160+
161+
When an implementation is added or modified, please review the following guidelines:
162+
163+
##### Docstrings
164+
Add module level description in form of a docstring with links to corresponding references or other useful information.
165+
Add "Examples in Python ecosystem" section if you know some. It shows how patterns could be applied to real-world problems.
166+
[facade.py](patterns/structural/facade.py) has a good example of detailed description, but sometimes the shorter one as in [template.py](patterns/behavioral/template.py) would suffice.
167+
168+
##### Python 2 compatibility
169+
To see Python 2 compatible versions of some patterns please check-out the [legacy](https://github.com/faif/python-patterns/tree/legacy) tag.
170+
171+
##### Update README
172+
When everything else is done - update corresponding part of README.
173+
174+
##### Travis CI
175+
Please run the following before submitting a patch:
176+
- `black .` This lints your code.
177+
- Either `tox` or `tox -e ci37` for unit tests.
178+
- If you have a bash compatible shell, use `./lint.sh`.
179+
180+
## Contributing via issue triage [![Open Source Helpers](https://www.codetriage.com/faif/python-patterns/badges/users.svg)](https://www.codetriage.com/faif/python-patterns)
181+
You can triage issues and pull requests on [CodeTriage](https://www.codetriage.com/faif/python-patterns).

0 commit comments

Comments
 (0)