Skip to content

Commit 188f7d2

Browse files
docs: add mermaid diagrams (#469)
* docs: add mermaid diagrams * Update README.md * docs: add mermaid diagrams * docs: add mermaid diagrams * docs: add mermaid diagrams * docs: add mermaid diagrams * docs: add mermaid diagrams
1 parent 39708b9 commit 188f7d2

File tree

1 file changed

+92
-56
lines changed

1 file changed

+92
-56
lines changed

README.md

Lines changed: 92 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
|:-------:| ----------- |
@@ -19,8 +30,29 @@ __Creational Patterns__:
1930
| [lazy_evaluation](patterns/creational/lazy_evaluation.py) | lazily-evaluated property pattern in Python |
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) |
33+
| [singleton](patterns/creational/singleton.py) | restrict the instantiation of a class to one object |
34+
35+
## Structural Patterns
36+
37+
> Patterns that define **how classes and objects are composed** to form larger, flexible structures.
38+
39+
```mermaid
40+
graph TD
41+
Client --> Facade
42+
Facade --> SubsystemA
43+
Facade --> SubsystemB
44+
Facade --> SubsystemC
45+
46+
Client2 --> Adapter
47+
Adapter --> LegacyService
48+
49+
Client3 --> Proxy
50+
Proxy -->|controls access to| RealSubject
2251
23-
__Structural Patterns__:
52+
Component --> Composite
53+
Composite --> Leaf1
54+
Composite --> Leaf2
55+
```
2456

2557
| Pattern | Description |
2658
|:-------:| ----------- |
@@ -35,14 +67,33 @@ __Structural Patterns__:
3567
| [mvc](patterns/structural/mvc.py) | model<->view<->controller (non-strict relationships) |
3668
| [proxy](patterns/structural/proxy.py) | an object funnels operations to something else |
3769

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+
```
3989

4090
| Pattern | Description |
4191
|:-------:| ----------- |
4292
| [chain_of_responsibility](patterns/behavioral/chain_of_responsibility.py) | apply a chain of successive handlers to try and process the data |
4393
| [catalog](patterns/behavioral/catalog.py) | general methods will call different specialized methods based on construction parameter |
4494
| [chaining_method](patterns/behavioral/chaining_method.py) | continue callback next object method |
4595
| [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 |
4697
| [iterator](patterns/behavioral/iterator.py) | traverse a container and access the container's elements |
4798
| [iterator](patterns/behavioral/iterator_alt.py) (alt. impl.)| traverse a container and access the container's elements |
4899
| [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__:
51102
| [publish_subscribe](patterns/behavioral/publish_subscribe.py) | a source syndicates events/data to 0+ registered listeners |
52103
| [registry](patterns/behavioral/registry.py) | keep track of all subclasses of a given class |
53104
| [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 |
55106
| [state](patterns/behavioral/state.py) | logic is organized into a discrete number of potential states and the next state that can be transitioned to |
56107
| [strategy](patterns/behavioral/strategy.py) | selectable operations over the same data |
57108
| [template](patterns/behavioral/template.py) | an object imposes a structure but takes pluggable components |
58109
| [visitor](patterns/behavioral/visitor.py) | invoke a callback for all items of a collection |
59110

60-
__Design for Testability Patterns__:
111+
## Design for Testability Patterns
61112

62113
| Pattern | Description |
63114
|:-------:| ----------- |
64115
| [dependency_injection](patterns/dependency_injection.py) | 3 variants of dependency injection |
65116

66-
__Fundamental Patterns__:
117+
## Fundamental Patterns
67118

68119
| Pattern | Description |
69120
|:-------:| ----------- |
70121
| [delegation_pattern](patterns/fundamental/delegation_pattern.py) | an object handles a request by delegating to a second object (the delegate) |
71122

72-
__Others__:
123+
## Others
73124

74125
| Pattern | Description |
75126
|:-------:| ----------- |
76127
| [blackboard](patterns/other/blackboard.py) | architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern |
77128
| [graph_search](patterns/other/graph_search.py) | graphing algorithms - non gang of four pattern |
78129
| [hsm](patterns/other/hsm/hsm.py) | hierarchical state machine - non gang of four pattern |
79130

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-
125131
## 🚫 Anti-Patterns
126132

127133
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
144150
- Prefer composition and delegation.
145151
- “Favor composition over inheritance.”
146152

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 [![Open Source Helpers](https://www.codetriage.com/faif/python-patterns/badges/users.svg)](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

Comments
 (0)