You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A collection of design patterns and idioms in Python.
5
4
6
5
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.
7
6
8
-
Current Patterns
9
-
----------------
7
+
## Creational Patterns
10
8
11
-
__Creational Patterns__:
9
+
> Patterns that deal with **object creation** — abstracting and controlling how instances are made.
|[proxy](patterns/structural/proxy.py)| an object funnels operations to something else |
37
69
38
-
__Behavioral Patterns__:
70
+
## Behavioral Patterns
71
+
72
+
> Patterns concerned with **communication and responsibility** between objects.
73
+
74
+
```mermaid
75
+
graph LR
76
+
Sender -->|sends event| Observer1
77
+
Sender -->|sends event| Observer2
78
+
79
+
Request --> Handler1
80
+
Handler1 -->|passes if unhandled| Handler2
81
+
Handler2 -->|passes if unhandled| Handler3
82
+
83
+
Context -->|delegates to| Strategy
84
+
Strategy -->|executes| Algorithm
85
+
86
+
Originator -->|saves state to| Memento
87
+
Caretaker -->|holds| Memento
88
+
```
39
89
40
90
| Pattern | Description |
41
91
|:-------:| ----------- |
42
92
|[chain_of_responsibility](patterns/behavioral/chain_of_responsibility.py)| apply a chain of successive handlers to try and process the data |
43
93
|[catalog](patterns/behavioral/catalog.py)| general methods will call different specialized methods based on construction parameter |
44
94
|[chaining_method](patterns/behavioral/chaining_method.py)| continue callback next object method |
45
95
|[command](patterns/behavioral/command.py)| bundle a command and arguments to call later |
96
+
|[interpreter](patterns/behavioral/interpreter.py)| define a grammar for a language and use it to interpret statements |
46
97
|[iterator](patterns/behavioral/iterator.py)| traverse a container and access the container's elements |
47
98
|[iterator](patterns/behavioral/iterator_alt.py) (alt. impl.)| traverse a container and access the container's elements |
48
99
|[mediator](patterns/behavioral/mediator.py)| an object that knows how to connect other objects and act as a proxy |
@@ -51,77 +102,32 @@ __Behavioral Patterns__:
51
102
|[publish_subscribe](patterns/behavioral/publish_subscribe.py)| a source syndicates events/data to 0+ registered listeners |
52
103
|[registry](patterns/behavioral/registry.py)| keep track of all subclasses of a given class |
53
104
|[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 |
105
+
|[specification](patterns/behavioral/specification.py)| business rules can be recombined by chaining the business rules together using boolean logic |
55
106
|[state](patterns/behavioral/state.py)| logic is organized into a discrete number of potential states and the next state that can be transitioned to |
56
107
|[strategy](patterns/behavioral/strategy.py)| selectable operations over the same data |
57
108
|[template](patterns/behavioral/template.py)| an object imposes a structure but takes pluggable components |
58
109
|[visitor](patterns/behavioral/visitor.py)| invoke a callback for all items of a collection |
59
110
60
-
__Design for Testability Patterns__:
111
+
## Design for Testability Patterns
61
112
62
113
| Pattern | Description |
63
114
|:-------:| ----------- |
64
115
|[dependency_injection](patterns/dependency_injection.py)| 3 variants of dependency injection |
65
116
66
-
__Fundamental Patterns__:
117
+
## Fundamental Patterns
67
118
68
119
| Pattern | Description |
69
120
|:-------:| ----------- |
70
121
|[delegation_pattern](patterns/fundamental/delegation_pattern.py)| an object handles a request by delegating to a second object (the delegate) |
71
122
72
-
__Others__:
123
+
## Others
73
124
74
125
| Pattern | Description |
75
126
|:-------:| ----------- |
76
127
|[blackboard](patterns/other/blackboard.py)| architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern |
77
128
|[graph_search](patterns/other/graph_search.py)| graphing algorithms - non gang of four pattern |
78
129
|[hsm](patterns/other/hsm/hsm.py)| hierarchical state machine - non gang of four pattern |
79
130
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 [](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
-
125
131
## 🚫 Anti-Patterns
126
132
127
133
This section lists some common design patterns that are **not recommended** in Python and explains why.
@@ -144,3 +150,33 @@ This section lists some common design patterns that are **not recommended** in P
144
150
- Prefer composition and delegation.
145
151
- “Favor composition over inheritance.”
146
152
153
+
## Videos
154
+
155
+
*[Design Patterns in Python by Peter Ullrich](https://www.youtube.com/watch?v=bsyjSW46TDg)
156
+
*[Sebastian Buczyński - Why you don't need design patterns in Python?](https://www.youtube.com/watch?v=G5OeYHCJuv0)
157
+
*[You Don't Need That!](https://www.youtube.com/watch?v=imW-trt0i9I)
158
+
*[Pluggable Libs Through Design Patterns](https://www.youtube.com/watch?v=PfgEU3W0kyU)
159
+
160
+
## Contributing
161
+
162
+
When an implementation is added or modified, please review the following guidelines:
163
+
164
+
##### Docstrings
165
+
Add module level description in form of a docstring with links to corresponding references or other useful information.
166
+
Add "Examples in Python ecosystem" section if you know some. It shows how patterns could be applied to real-world problems.
167
+
[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.
168
+
169
+
##### Python 2 compatibility
170
+
To see Python 2 compatible versions of some patterns please check-out the [legacy](https://github.com/faif/python-patterns/tree/legacy) tag.
171
+
172
+
##### Update README
173
+
When everything else is done - update corresponding part of README.
174
+
175
+
##### Travis CI
176
+
Please run the following before submitting a patch:
177
+
-`black .` This lints your code.
178
+
- Either `tox` or `tox -e ci37` for unit tests.
179
+
- If you have a bash compatible shell, use `./lint.sh`.
180
+
181
+
## Contributing via issue triage [](https://www.codetriage.com/faif/python-patterns)
182
+
You can triage issues and pull requests on [CodeTriage](https://www.codetriage.com/faif/python-patterns).
0 commit comments