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
68
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
+
```
39
88
40
89
| Pattern | Description |
41
90
|:-------:| ----------- |
42
91
|[chain_of_responsibility](patterns/behavioral/chain_of_responsibility.py)| apply a chain of successive handlers to try and process the data |
43
92
|[catalog](patterns/behavioral/catalog.py)| general methods will call different specialized methods based on construction parameter |
44
93
|[chaining_method](patterns/behavioral/chaining_method.py)| continue callback next object method |
45
94
|[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 |
46
96
|[iterator](patterns/behavioral/iterator.py)| traverse a container and access the container's elements |
47
97
|[iterator](patterns/behavioral/iterator_alt.py) (alt. impl.)| traverse a container and access the container's elements |
48
98
|[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__:
51
101
|[publish_subscribe](patterns/behavioral/publish_subscribe.py)| a source syndicates events/data to 0+ registered listeners |
52
102
|[registry](patterns/behavioral/registry.py)| keep track of all subclasses of a given class |
53
103
|[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 |
55
105
|[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
106
|[strategy](patterns/behavioral/strategy.py)| selectable operations over the same data |
57
107
|[template](patterns/behavioral/template.py)| an object imposes a structure but takes pluggable components |
58
108
|[visitor](patterns/behavioral/visitor.py)| invoke a callback for all items of a collection |
59
109
60
-
__Design for Testability Patterns__:
110
+
## Design for Testability Patterns
61
111
62
112
| Pattern | Description |
63
113
|:-------:| ----------- |
64
114
|[dependency_injection](patterns/dependency_injection.py)| 3 variants of dependency injection |
65
115
66
-
__Fundamental Patterns__:
116
+
## Fundamental Patterns
67
117
68
118
| Pattern | Description |
69
119
|:-------:| ----------- |
70
120
|[delegation_pattern](patterns/fundamental/delegation_pattern.py)| an object handles a request by delegating to a second object (the delegate) |
71
121
72
-
__Others__:
122
+
## Others
73
123
74
124
| Pattern | Description |
75
125
|:-------:| ----------- |
76
126
|[blackboard](patterns/other/blackboard.py)| architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern |
77
127
|[graph_search](patterns/other/graph_search.py)| graphing algorithms - non gang of four pattern |
78
128
|[hsm](patterns/other/hsm/hsm.py)| hierarchical state machine - non gang of four pattern |
79
129
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
130
## 🚫 Anti-Patterns
126
131
127
132
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
144
149
- Prefer composition and delegation.
145
150
- “Favor composition over inheritance.”
146
151
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 [](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