Skip to content

Commit d38dd96

Browse files
committed
updates
1 parent d9d0e25 commit d38dd96

File tree

318 files changed

+4656
-4480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

318 files changed

+4656
-4480
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode/
2-
site/
2+
site/
3+
__pycache__/

Diff for: README.md

+76-27
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,86 @@
11
# Design Patterns In Python
22

3-
This course is about common GOF (Gang Of Four) Design Patterns implemented in Python.
3+
This repository focuses on the 23 famous GoF (Gang of Four) Design Patterns implemented in Python.
4+
5+
It is supplementary to my book titled **Design Patterns In Python**.
6+
7+
<table>
8+
<tr>
9+
<td>
10+
<img style="width:175;height=250;" src="./img/design_patterns_in_python_book.jpg">
11+
</td>
12+
<td>
13+
<a href="https://www.amazon.com/dp/B08XLJ8Z2J"><img src="./img/flag_us.gif">&nbsp; https://www.amazon.com/dp/B08XLJ8Z2J</a><br/>
14+
<a href="https://www.amazon.co.uk/dp/B08XLJ8Z2J"><img src="./img/flag_uk.gif">&nbsp; https://www.amazon.co.uk/dp/B08XLJ8Z2J</a><br/>
15+
<a href="https://www.amazon.de/dp/B08XLJ8Z2J"><img src="./img/flag_de.gif">&nbsp; https://www.amazon.de/dp/B08XLJ8Z2J</a><br/>
16+
<a href="https://www.amazon.fr/dp/B08XLJ8Z2J"><img src="./img/flag_fr.gif">&nbsp; https://www.amazon.fr/dp/B08XLJ8Z2J</a><br/>
17+
<a href="https://www.amazon.es/dp/B08XLJ8Z2J"><img src="./img/flag_es.gif">&nbsp; https://www.amazon.es/dp/B08XLJ8Z2J</a><br/>
18+
<a href="https://www.amazon.it/dp/B08XLJ8Z2J"><img src="./img/flag_it.gif">&nbsp; https://www.amazon.it/dp/B08XLJ8Z2J</a><br/>
19+
<a href="https://www.amazon.co.jp/dp/B08XLJ8Z2J"><img src="./img/flag_jp.gif">&nbsp; https://www.amazon.co.jp/dp/B08XLJ8Z2J</a><br/>
20+
<a href="https://www.amazon.ca/dp/B08XLJ8Z2J"><img src="./img/flag_ca.gif">&nbsp; https://www.amazon.ca/dp/B08XLJ8Z2J</a>
21+
</td>
22+
</tr>
23+
</table>
24+
25+
All the code examples in the book can be found in these pages.
426

527
A Design Pattern is a description or template that can be repeatedly applied to a commonly recurring problem in software design.
628

7-
You will find a familiarity with Design Patterns very useful when planning, discussing, developing, managing and documenting your applications from now on and into the future.
29+
A familiarity of Design Patterns will be very useful when planning, discussing, managing and documenting your applications from now on and into the future.
830

9-
You will learn these Design Patterns
31+
Also, throughout the book, as each design pattern is discussed and demonstrated using example code, I also introduce new python coding concepts with each new design pattern. So that as you progress through the book and try out the examples, you will also get experience and familiarity with some of the finer details of programming with python.
32+
33+
I had fun writing this book and coming up with new examples, and I hope you enjoy it as well.
34+
35+
So, in this book, you will learn about these 23 Design Patterns,
1036

1137
* Creational
12-
* [Factory](factory)
13-
* [Abstract Factory](abstract_factory)
14-
* [Builder](builder)
15-
* [Prototype](prototype)
16-
* Singleton
38+
- [Factory](factory)
39+
- [Abstract Factory](abstract_factory)
40+
- [Builder](builder)
41+
- [Prototype](prototype)
42+
- [Singleton](singleton)
1743
* Structural
18-
* [Decorator](decorator)
19-
* [Adapter](adapter)
20-
* [Facade](facade)
21-
* [Bridge](bridge)
22-
* [Composite](composite)
23-
* Flyweight
24-
* [Proxy](proxy)
25-
* Behavioural
26-
* [Command](command)
27-
* [Chain of Responsibility](chain_of_responsibility)
28-
* [Observer Pattern](observer)
29-
* Interpreter
30-
* [Iterator](iterator)
31-
* [Mediator](mediator)
32-
* Memento
33-
* State
34-
* Strategy
35-
* Template
36-
* Visitor
44+
- [Decorator](decorator)
45+
- [Adapter](adapter)
46+
- [Facade](facade)
47+
- [Bridge](bridge)
48+
- [Composite](composite)
49+
- [Flyweight](flyweight)
50+
- [Proxy](proxy)
51+
* Behavioral
52+
- [Command](command)
53+
- [Chain of Responsibility](chain_of_responsibility)
54+
- [Observer Pattern](observer)
55+
- [Interpreter](interpreter)
56+
- [Iterator](iterator)
57+
- [Mediator](mediator)
58+
- [Memento](memento)
59+
- [State](state)
60+
- [Strategy](strategy)
61+
- [Template](template)
62+
- [Visitor](visitor)
63+
64+
## Pattern Types
65+
66+
In the list of patterns above, there are Creational, Structural and Behavioral patterns.
67+
68+
* **Creational** : Abstracts the instantiation process so that there is a logical separation between how objects are composed and finally represented.
69+
* **Structural** : Structural patterns focus more on how classed and objects are composed using the different structural techniques, and to form structures with more or altered flexibility.
70+
* **Behavioral** : Are concerned with the inner algorithms, process flow, the assignment of responsibilities and the intercommunication between objects.
71+
72+
## Class Scope and Object Scope Patterns
73+
74+
Each pattern can be further specified whether it relates more specifically classes or instantiated objects.
75+
76+
Class scope patterns deal more with relationships between classes and their subclasses.
77+
78+
Object scope patterns deal more with relationships that can be altered at runtime
3779

80+
| Pattern | Description | Scope | Type |
81+
|---------------------------------------------------------------------------------------------------|--------------------------------------------|--------|-------------|
82+
| Factory, Abstract Factory | Defers object creation to subclasses | Class | Creational |
83+
| Builder, Prototype, Singleton | Defers object creation to objects | Object | Creational |
84+
| Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy | Describes a way to assemble objects | Object | Structural |
85+
| Interpreter, Template | Describes algorithms and flow control | Class | Behavioral |
86+
| Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor | Describes how groups of objects co-operate | Object | Behavioral |

Diff for: abstract_factory/Abstract Factory Design Pattern.md

-59
This file was deleted.

Diff for: abstract_factory/Abstract Factory Design Pattern.pdf

-166 KB
Binary file not shown.

Diff for: abstract_factory/README.md

-62
This file was deleted.
-2.91 KB
Binary file not shown.
-2.93 KB
Binary file not shown.

Diff for: abstract_factory/abstract_factory.png

-16 KB
Binary file not shown.

Diff for: abstract_factory/abstract_factory_concept.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# pylint: disable=too-few-public-methods
2+
"Abstract Factory Concept Sample Code"
3+
from abc import ABCMeta, abstractmethod
4+
from factory_a import FactoryA
5+
from factory_b import FactoryB
6+
7+
8+
class IAbstractFactory(metaclass=ABCMeta):
9+
"Abstract Factory Interface"
10+
11+
@staticmethod
12+
@abstractmethod
13+
def create_object(factory):
14+
"The static Abstract factory interface method"
15+
16+
17+
class AbstractFactory(IAbstractFactory):
18+
"The Abstract Factory Concrete Class"
19+
20+
@staticmethod
21+
def create_object(factory):
22+
"Static get_factory method"
23+
try:
24+
if factory in ['aa', 'ab', 'ac']:
25+
return FactoryA().create_object(factory[1])
26+
if factory in ['ba', 'bb', 'bc']:
27+
return FactoryB().create_object(factory[1])
28+
raise Exception('No Factory Found')
29+
except Exception as _e:
30+
print(_e)
31+
return None
32+
33+
34+
# The Client
35+
PRODUCT = AbstractFactory.create_object('ab')
36+
print(f"{PRODUCT.__class__}")
37+
38+
PRODUCT = AbstractFactory.create_object('bc')
39+
print(f"{PRODUCT.__class__}")

Diff for: abstract_factory/abstract_factory_furniture.png

-70.2 KB
Binary file not shown.

Diff for: abstract_factory/big_chair.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"A Class of Chair"
2+
from interface_chair import IChair
3+
4+
5+
class BigChair(IChair): # pylint: disable=too-few-public-methods
6+
"The Big Chair Concrete Class which implements the IChair interface"
7+
8+
def __init__(self):
9+
self._height = 80
10+
self._width = 80
11+
self._depth = 80
12+
13+
def get_dimensions(self):
14+
return {
15+
"width": self._width,
16+
"depth": self._depth,
17+
"height": self._height
18+
}

Diff for: abstract_factory/big_table.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"A Class of Table"
2+
from interface_table import ITable
3+
4+
5+
class BigTable(ITable): # pylint: disable=too-few-public-methods
6+
"The Big Chair Concrete Class implements the ITable interface"
7+
8+
def __init__(self):
9+
self._height = 60
10+
self._width = 120
11+
self._depth = 80
12+
13+
def get_dimensions(self):
14+
return {
15+
"width": self._width,
16+
"depth": self._depth,
17+
"height": self._height
18+
}

0 commit comments

Comments
 (0)