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
@@ -25,11 +33,17 @@ Describes most useful python design patterns.
25
33
-[Strategy](#strategy)
26
34
-[Chain of responsibility](#chain-of-responsibility)
27
35
-[Other qualities](#other-qualities)
36
+
-[Development notes](#development-notes)
37
+
-[Release notes](#release-notes)
38
+
-[Meta](#meta)
39
+
-[Contributing](#contributing)
28
40
29
41
## Creational
30
-
Used to create objects in a systematic way. Supports flexibility and different subtypes of objects from the same class at runtime. Here polymorphism is often used.
42
+
Creational types of patterns used to create objects in a systematic way. Supports flexibility and different subtypes of objects from the same class at runtime.
43
+
Here **_polymorphism_** is often used.
44
+
31
45
### Factory method
32
-
Define an interface for creating an object but defer object instantiation to run time.
46
+
Factory method defines an interface for creating an object but defers object instantiation to run time.
33
47
```python
34
48
from abc importABC, abstractmethod
35
49
@@ -139,14 +153,14 @@ get_pet('cat')
139
153
140
154
```
141
155
### Abstract factory
142
-
Client expects to receive family related objects. But dont have to know which family it is until run time. Abstract factory is related to factory method and concrete product are singletons.
143
-
-Participants:
144
-
- Abstract factory: pet factory.
145
-
- Concrete factory: dog factory and cat factory.
146
-
- Abstract product.
147
-
- Concrete product: dog and dog food, cat and cat food.
156
+
In abstract factory a client expects to receive family related objects. But don't have to know which family it is until run time. Abstract factory is related to factory method and concrete product are singletons.
157
+
-Implementation idea:
158
+
- Abstract factory: pet factory
159
+
- Concrete factory: dog factory and cat factory
160
+
- Abstract product
161
+
- Concrete product: dog and dog food, cat and cat food
148
162
- Exercise:
149
-
We have a Pet factory (which includes Dog and Cat factory and both factories produced related products such as Dog and Cat food and we have a PetFactory which gets Cat or Dog factory).
163
+
-We have a Pet factory (which includes Dog and Cat factory and both factories produced related products such as Dog and Cat food and we have a PetFactory which gets Cat or Dog factory).
150
164
151
165
```python
152
166
from abc importABC, abstractmethod
@@ -286,10 +300,10 @@ store.show_pet()
286
300
287
301
```
288
302
### Singleton
289
-
Global variable, modules - singleton. Allows only one object to be instantiated from a class template.
290
-
If you want to share cached information to multiple objects.
303
+
Python has global variables and modules which are **_singletons_**. Singleton allows only one object to be instantiated from a class template.
304
+
Useful if you want to share cached information to multiple objects.
291
305
292
-
Classic singleton
306
+
**Classic singleton**
293
307
```python
294
308
classSingleton:
295
309
"""Makes all instances as the same object."""
@@ -333,7 +347,7 @@ print(id(bar_two))
333
347
print(bar_one is bar_two)
334
348
```
335
349
336
-
Borg singleton
350
+
**Borg singleton**
337
351
```python
338
352
from typing import Any
339
353
@@ -369,13 +383,14 @@ print(y)
369
383
```
370
384
371
385
### Builder
372
-
Reduces complexity of building objects.
386
+
Builder reduces complexity of building objects.
373
387
- Participants:
374
-
- Director.
375
-
- Abstract Builder: interfaces.
376
-
- Concrete Builder: implements the interfaces.
377
-
- Product: object being built.
378
-
- Exercise to to build a car object.
388
+
- Director
389
+
- Abstract Builder: interfaces
390
+
- Concrete Builder: implements the interfaces
391
+
- Product: object being built
392
+
- Exercise:
393
+
- Build a car object
379
394
380
395
```python
381
396
from abc importABC, abstractmethod
@@ -463,12 +478,12 @@ print(car.summary())
463
478
464
479
```
465
480
### Prototype
466
-
Prototype patterns is related to abstract factory pattern.
481
+
Prototype patterns are related to abstract factory pattern.
467
482
- Ideas:
468
-
- Clone objects according to prototypical instance.
469
-
- Creating many identical objects individually.
470
-
- Clone individual objects.
471
-
- Create a prototypical instance first.
483
+
- Clone objects according to prototypical instance.
484
+
- Creating many identical objects individually.
485
+
- Clone individual objects
486
+
- Create a prototypical instance first
472
487
- Exercise:
473
488
- Use the same car if car has same color or options, you can clone objects instead of creating individual objects
474
489
@@ -527,13 +542,14 @@ print(cloned_car.summary())
527
542
```
528
543
529
544
## Structural
530
-
Establish useful relationships between software components. Here inheritance is often used.
531
-
- Route maps the user request to a `Controller` which...
532
-
- Uses the `Model` to retrieve all of the necessary data, organizes it and send it off to the...
533
-
- View, which then uses that data to render the web page.
545
+
Structural type of patterns establish useful relationships between software components. Here **_inheritance_** is often used.
546
+
- Ideas:
547
+
- Route maps the user request to a `Controller` which...
548
+
- Uses the `Model` to retrieve all of the necessary data, organizes it and send it off to the...
549
+
- View, which then uses that data to render the web page
534
550
535
551
### MVC
536
-
UI pattern intended to separate internal representation of data from ways it is presented to/accepted from the user
552
+
MVC (Model-View-Controller) is a UI pattern intended to separate internal representation of data from ways it is presented to/from the user.
537
553
538
554
```python
539
555
from abc importABC, abstractmethod
@@ -703,11 +719,11 @@ if __name__ == "__main__":
703
719
```
704
720
705
721
### Decorator
706
-
Add new feature to an existing object. Supports dynamic changes.
722
+
Decorator type of patterns add new feature to an existing object. Supports dynamic changes.
707
723
- Exercise:
708
724
- Add additional message to an existing function
709
725
710
-
Decorator function
726
+
**Decorator function**
711
727
```python
712
728
from functools import wraps
713
729
from typing import Callable
@@ -735,7 +751,7 @@ print(hello_world.__name__)
735
751
print(hello_world.__doc__)
736
752
```
737
753
738
-
Decorator class
754
+
**Decorator class**
739
755
```python
740
756
from abc importABC, abstractmethod
741
757
@@ -786,7 +802,7 @@ print(sum_float.value())
786
802
```
787
803
788
804
### Proxy
789
-
Postpone object creation unless it is necessary. Object is too expensive (resource intensive) to create that's why we have to create it if it is needed.
805
+
Proxy patterns postpones object creation unless it is necessary. Object is too expensive (resource intensive) to create that's why we have to create it once it is needed.
790
806
- Participants:
791
807
- Producer
792
808
- Artist
@@ -841,11 +857,11 @@ proxy.produce()
841
857
```
842
858
843
859
### Adapter
844
-
Converts interface of a class into another one a client is expecting.
860
+
Adapter patterns converts interface of a class into another one a client is expecting.
845
861
- Exercise:
846
-
- Korean language: speak_korean()
847
-
- British language: speak_english()
848
-
- Client has to have uniform interface - speak method()
862
+
- Korean language: `speak_korean()`
863
+
- British language: `speak_english()`
864
+
- Client has to have uniform interface - `speak method`
849
865
- Solution:
850
866
- Use an adapter pattern that translates method name between client and the server code
851
867
@@ -914,7 +930,7 @@ for speaker in speakers:
914
930
- Participants:
915
931
- Component - abstract 'class'
916
932
- Child - inherits from Component 'class'
917
-
- Composite - inherits from component 'class'. Maintain child objects by adding.removing them.
933
+
- Composite - inherits from component 'class'. Maintain child objects by adding.removing them
918
934
919
935
```python
920
936
from abc importABC, abstractmethod
@@ -978,7 +994,8 @@ top_menu.function()
978
994
```
979
995
980
996
### Bridge
981
-
Separates the abstraction into different class hierarchies. Abstract factory and adapter patterns are related to this Bridge design pattern.
997
+
Bridge pattern separates the abstraction into different class hierarchies.
998
+
Abstract factory and adapter patterns are related to Bridge design pattern.
982
999
983
1000
```python
984
1001
from abc importABC, abstractmethod
@@ -1043,7 +1060,8 @@ circle_two.draw()
1043
1060
```
1044
1061
1045
1062
### Facade
1046
-
The Facade pattern is a way to provide a simpler unified interface to a more complex system. It provides an easier way to access functions of the underlying system by providing a single entry point.
1063
+
The Facade pattern is a way to provide a simpler unified interface to a more complex system.
1064
+
It provides an easier way to access functions of the underlying system by providing a single entry point.
1047
1065
1048
1066
```python
1049
1067
from typing import Tuple, Iterator
@@ -1185,13 +1203,13 @@ test_suite.run()
1185
1203
```
1186
1204
1187
1205
## Behavioral
1188
-
Best practices of objects interaction. Methods and signatures are often used.
1206
+
Behavioral patterns provide best practices of objects interaction. Methods and signatures are often used.
1189
1207
1190
1208
### Observer
1191
-
Establishes one yo many relationship between subject and multiple observers. Singleton is related to observer design pattern.
1209
+
Observer pattern establishes one to many relationship between subject and multiple observers. Singleton is related to observer design pattern.
1192
1210
- Exercise:
1193
-
- Subjects need to be monitored.
1194
-
- Observers need to be notified.
1211
+
- Subjects need to be monitored
1212
+
- Observers need to be notified
1195
1213
- Participants:
1196
1214
- Subject: abstract class
1197
1215
- Attach
@@ -1262,7 +1280,7 @@ subject_one.notify()
1262
1280
```
1263
1281
1264
1282
### Visitor
1265
-
Add new features to existing hierarchy without changing it. Add new operations to existing classes dynamically.
1283
+
Visitor pattern adds new features to existing hierarchy without changing it. Add new operations to existing classes dynamically.
1266
1284
Exercise:
1267
1285
- House class:
1268
1286
- HVAC specialist: Visitor type 1
@@ -1339,10 +1357,10 @@ home.accept(electrician)
1339
1357
### Iterator
1340
1358
Composite pattern is related to iterator pattern.
1341
1359
- Exercise:
1342
-
- Our custom iterator based on a build-in python iterator: `zip()`.
1343
-
- Will iterate over a certain point baed on client input.
1360
+
- Our custom iterator based on a build-in python iterator: `zip()`
1361
+
- Will iterate over a certain point based on client input
1344
1362
1345
-
Iterator function
1363
+
**Iterator function**
1346
1364
```python
1347
1365
from typing import Iterator, Tuple
1348
1366
@@ -1359,7 +1377,7 @@ for number in count_to(3):
1359
1377
print("{} in german is {}".format(*number))
1360
1378
```
1361
1379
1362
-
Iterator class
1380
+
**Iterator class**
1363
1381
```python
1364
1382
from typing import Iterator
1365
1383
@@ -1383,7 +1401,7 @@ for _ in range(10):
1383
1401
```
1384
1402
1385
1403
### Strategy
1386
-
Dynamically changing the behavior of an object. Add dynamically objects with `types` module.
1404
+
Strategy patterns used to dynamically change the behavior of an object. Add dynamically objects with `types` module.
1387
1405
- Participants:
1388
1406
- Abstract strategy class with default set of behaviors
1389
1407
- Concrete strategy class with new behaviors
@@ -1436,7 +1454,7 @@ second_strategy.execute()
1436
1454
```
1437
1455
1438
1456
### Chain of responsibility
1439
-
Decouple responsibility. Composite is related to this design pattern.
1457
+
Thiss type of pattern decouples responsibility. Composite is related to this design pattern.
1440
1458
- Exercise:
1441
1459
- Integer value
1442
1460
- Handlers
@@ -1514,14 +1532,25 @@ c.delegate(requests)
1514
1532
1515
1533
**Cohesion** refers to how independent the software component is. More cohesion is better.
1516
1534
1535
+
## Development notes
1536
+
1537
+
### Release notes
1538
+
1539
+
* 0.1.1
1540
+
* Polish documentation
1541
+
* 0.1.0
1542
+
* Distribute first version of a project
1543
+
1544
+
### Meta
1545
+
Author – Volodymyr Yahello vyahello@gmail.com
1546
+
1547
+
Distributed under the `MIT` license. See [LICENSE](LICENSE.md) for more information.
0 commit comments