Example code from [Head First Design Patterns second edition](https://www.wickedlysmart.com/head-first-design-patterns/) translated to python to help me understand and memorise the patterns.
I have added examples of pattern usage in the Python standard library and pypi - I am starting to see patterns everywhere!
> **Note**
-> I am aiming for a reasonably literal translation whilst trying to make the code a little more pythonic by, e.g. using python conventions for `ClassNames` and `method_names` and putting all of the code in a single file where it makes sense to do so.
+> I am aiming for a mostly literal translation whilst trying to make the code a little more pythonic by, e.g. using python conventions for `ClassNames` and `method_names` and putting all of the code in a single file where it makes sense to do so.
## Patterns Implemented
From 21790e98710dbab6f4116b6f1c9ec9aa87038ad4 Mon Sep 17 00:00:00 2001
From: Graham Knapp <32717635+dancergraham@users.noreply.github.com>
Date: Tue, 21 Feb 2023 21:44:35 +0100
Subject: [PATCH 04/28] =?UTF-8?q?doc:=20builder=20pattern=20=F0=9F=91=B7?=
=?UTF-8?q?=F0=9F=8F=BB=E2=80=8D=E2=99=80=EF=B8=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
keep on building
---
chapter04_factory/readme.md | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/chapter04_factory/readme.md b/chapter04_factory/readme.md
index 9361761..98e6a1b 100644
--- a/chapter04_factory/readme.md
+++ b/chapter04_factory/readme.md
@@ -24,6 +24,11 @@ Rhino.
For instance the `PizzaIngredientFactory` abstract class defines an interface for a family of products.
+** Builder **
+
+When the object creation gets more complex with a number of distinct steps then the Builder pattern comes in,
+esseantially using a Template method to put all of the creation steps together.
+
## Running the code
```bash
From 665da9d192c7307ca748ee917cb1b64034bd08ec Mon Sep 17 00:00:00 2001
From: Graham Knapp <32717635+dancergraham@users.noreply.github.com>
Date: Tue, 21 Feb 2023 21:45:12 +0100
Subject: [PATCH 05/28] =?UTF-8?q?doc:=20builder=20pattern=20=F0=9F=91=B7?=
=?UTF-8?q?=F0=9F=8F=BB=E2=80=8D=E2=99=80=EF=B8=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
chapter04_factory/readme.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/chapter04_factory/readme.md b/chapter04_factory/readme.md
index 98e6a1b..fc69209 100644
--- a/chapter04_factory/readme.md
+++ b/chapter04_factory/readme.md
@@ -24,7 +24,7 @@ Rhino.
For instance the `PizzaIngredientFactory` abstract class defines an interface for a family of products.
-** Builder **
+**Builder 👷🏻♀️**
When the object creation gets more complex with a number of distinct steps then the Builder pattern comes in,
esseantially using a Template method to put all of the creation steps together.
From c0b777526377b8dbf88b5217642d6f584267a5be Mon Sep 17 00:00:00 2001
From: Graham Knapp <32717635+dancergraham@users.noreply.github.com>
Date: Sat, 18 Mar 2023 09:17:48 +0100
Subject: [PATCH 06/28] doc: factory documentation and examples
Experimenting with new format
---
chapter04_factory/readme.md | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/chapter04_factory/readme.md b/chapter04_factory/readme.md
index fc69209..88d9f17 100644
--- a/chapter04_factory/readme.md
+++ b/chapter04_factory/readme.md
@@ -1,30 +1,41 @@
# Chapter 4: Factory patterns
-> **Simple Factory**: A class which chooses which product class to instantiate and return, based upon method parameters.
+## Simple Factory
-The Python standard library contains multiple references to factory objects, for instances
-in [dataclasses](https://docs.python.org/3/library/dataclasses.html?highlight=factory).
-The Factory Boy package provides easy object creation for Django
-and for other ORMs.
+> A class which chooses which product class to instantiate and return, based upon method parameters.
-> **Factory Method**: Defines an interface for creating an object, but lets subclasses decide which class to
+### Pythonic Examples
+
+The Python standard library contains multiple references to factory objects, for instance
+[namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple)
+and [dataclasses](https://docs.python.org/3/library/dataclasses.html#module-dataclasses)
+are factories for creating classes.
+The Factory Boy package provides easy object creation for Django and for other ORMs.
+
+## Factory Method
+
+> Defines an interface for creating an object, but lets subclasses decide which class to
> instantiate. The Factory method lets a class defer instantiation to subclasses.
For instance the `PizzaStore` abstract class in this repo provides an abstract `create_pizza` interface for creating one
product.
+### Pythonic Examples
+
The [python-qrcode](https://github.com/dancergraham/python-qrcode) module uses the factory method pattern nicely to
separate only the part of the code that changes (generating png, svg, etc.) from the underlying logic of the code
generation and to allow extension through the creation of new factory methods without modification of the existing code.
I took advantage of this to add a new class for the creation of 3D QR codes with my favourite NURBS modelling software
-Rhino.
+Rhino - it was super simple once I understood the pattern.
+
+## Abstract Factory
-> **Abstract Factory**: Provides an interface for creating families of related or dependent objects without specifying
+> Provides an interface for creating families of related or dependent objects without specifying
> their concrete classes.
For instance the `PizzaIngredientFactory` abstract class defines an interface for a family of products.
-**Builder 👷🏻♀️**
+## Builder 👷🏻♀️
When the object creation gets more complex with a number of distinct steps then the Builder pattern comes in,
esseantially using a Template method to put all of the creation steps together.
From 094a49410b0eee50f1b918f2363d5e25d762ea1d Mon Sep 17 00:00:00 2001
From: Graham Knapp <32717635+dancergraham@users.noreply.github.com>
Date: Sat, 18 Mar 2023 09:22:34 +0100
Subject: [PATCH 07/28] =?UTF-8?q?doc:=20emojis=20=F0=9F=A5=B3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
chapter04_factory/readme.md | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/chapter04_factory/readme.md b/chapter04_factory/readme.md
index 88d9f17..f9c0d6b 100644
--- a/chapter04_factory/readme.md
+++ b/chapter04_factory/readme.md
@@ -1,6 +1,6 @@
# Chapter 4: Factory patterns
-## Simple Factory
+## Simple Factory 🚧
> A class which chooses which product class to instantiate and return, based upon method parameters.
@@ -12,7 +12,7 @@ and [dataclasses](https://docs.python.org/3/library/dataclasses.html#module-data
are factories for creating classes.
The Factory Boy package provides easy object creation for Django and for other ORMs.
-## Factory Method
+## Factory Method 📋
> Defines an interface for creating an object, but lets subclasses decide which class to
> instantiate. The Factory method lets a class defer instantiation to subclasses.
@@ -28,14 +28,14 @@ generation and to allow extension through the creation of new factory methods wi
I took advantage of this to add a new class for the creation of 3D QR codes with my favourite NURBS modelling software
Rhino - it was super simple once I understood the pattern.
-## Abstract Factory
+## Abstract Factory 🏭
> Provides an interface for creating families of related or dependent objects without specifying
> their concrete classes.
For instance the `PizzaIngredientFactory` abstract class defines an interface for a family of products.
-## Builder 👷🏻♀️
+## Builder 👷🏻♀️🏗️
When the object creation gets more complex with a number of distinct steps then the Builder pattern comes in,
esseantially using a Template method to put all of the creation steps together.
From c860aa899e8de14c3a430d17b2dbeba0f7c4bd6f Mon Sep 17 00:00:00 2001
From: dancergraham +h}SsuGu7e>`D`ZvDM=e$1EJDI_B5dDC1ywyFQOO
zZR9%!H+*Y+)X>d%koDAPAIsUOmwC@{AJfLLgK?L&oiU9iH7I0mFqp&?>K8CJ>02;_
zdie(H^(+j8y7~H>b;s-Tb@KH#>zL_@wJ+&z*0#{?(8|}@p=F`dsd+_vpQeSjOruci
zfW{OpQvHr*n7W;&l6r|of||32zS=AGbE@9zW~yJ+Zm7&tvsS5AeWJWt)k|5V@>OY<
z$`Ykv ^v_Ek?89Nl8X8qtWPjc!pBZ186L|16b#E=wV>p
zcLUqJ5%q&-$_1T|+M)iy$h)H^@VqgBg;qz+P!u&pN8kzVrqO|imeXo!J+vRdMZc%j
z!}I);c9-^nc8&I&cAj>hmO?84MmmS~H!YR6hZarSNIOnj4l@B9+9sM0Z3WGVHkUS?
z=1Vh&7!zXZGz%Jy#-a@ZU)@fn(VD1(R35b*-mxa?3o4g-pZbA%jryE=j(QLH>nqf7
zYBu!vCUHr;~
z@Ob?Mc6@ofLVQg8t23^@=fd32+&M!zv+2x>IFmES<7(nu
QDt#2h@ttSQ5YD@B14K+%O&^h_ul!H;Yo#hG%N;!jDSETWtTU$YyOy|BjK
zDau<&`2{J>kkUmdp%9dJ6g4W3Vgz1n6RACvSyT$um#PUKZ)2$IVEw(l;G24yItNyE
zTM8@iZKmD^zqserNa`1A2DKJ^=A_hz)DdbKRhjmS%A&PWC(?SU4&Zgirs>cY!&-5h
zXftVtXrAD=H;