|
1 | 1 | # Chapter 4: Factory patterns
|
2 | 2 |
|
3 |
| -> **Simple Factory**: A class which chooses which product class to instantiate and return, based upon method parameters. |
| 3 | +## Simple Factory 🚧 |
4 | 4 |
|
5 |
| -The Python standard library contains multiple references to factory objects, for instances |
6 |
| -in [dataclasses](https://docs.python.org/3/library/dataclasses.html?highlight=factory). |
7 |
| -The Factory Boy package provides easy object creation for Django |
8 |
| -and for other ORMs. |
| 5 | +> A class which chooses which product class to instantiate and return, based upon method parameters. |
9 | 6 |
|
10 |
| -> **Factory Method**: Defines an interface for creating an object, but lets subclasses decide which class to |
| 7 | +### Pythonic Examples |
| 8 | + |
| 9 | +The Python standard library contains multiple references to factory objects, for instance |
| 10 | +[namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) |
| 11 | +and [dataclasses](https://docs.python.org/3/library/dataclasses.html#module-dataclasses) |
| 12 | +are factories for creating classes. |
| 13 | +The Factory Boy package provides easy object creation for Django and for other ORMs. |
| 14 | + |
| 15 | +## Factory Method 📋 |
| 16 | + |
| 17 | +> Defines an interface for creating an object, but lets subclasses decide which class to |
11 | 18 | > instantiate. The Factory method lets a class defer instantiation to subclasses.
|
12 | 19 |
|
13 | 20 | For instance the `PizzaStore` abstract class in this repo provides an abstract `create_pizza` interface for creating one
|
14 | 21 | product.
|
15 | 22 |
|
| 23 | +### Pythonic Examples |
| 24 | + |
16 | 25 | The [python-qrcode](https://github.com/dancergraham/python-qrcode) module uses the factory method pattern nicely to
|
17 | 26 | separate only the part of the code that changes (generating png, svg, etc.) from the underlying logic of the code
|
18 | 27 | generation and to allow extension through the creation of new factory methods without modification of the existing code.
|
19 | 28 | I took advantage of this to add a new class for the creation of 3D QR codes with my favourite NURBS modelling software
|
20 |
| -Rhino. |
| 29 | +Rhino - it was super simple once I understood the pattern. |
| 30 | + |
| 31 | +## Abstract Factory 🏭 |
21 | 32 |
|
22 |
| -> **Abstract Factory**: Provides an interface for creating families of related or dependent objects without specifying |
| 33 | +> Provides an interface for creating families of related or dependent objects without specifying |
23 | 34 | > their concrete classes.
|
24 | 35 |
|
25 | 36 | For instance the `PizzaIngredientFactory` abstract class defines an interface for a family of products.
|
26 | 37 |
|
27 |
| -**Builder 👷🏻♀️** |
| 38 | +## Builder 👷🏻♀️🏗️ |
28 | 39 |
|
29 | 40 | When the object creation gets more complex with a number of distinct steps then the Builder pattern comes in,
|
30 | 41 | esseantially using a Template method to put all of the creation steps together.
|
|
0 commit comments