Skip to content

Commit 954c3b7

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents c860aa8 + 094a494 commit 954c3b7

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

chapter04_factory/readme.md

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
# Chapter 4: Factory patterns
22

3-
> **Simple Factory**: A class which chooses which product class to instantiate and return, based upon method parameters.
3+
## Simple Factory 🚧
44

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.
96
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
1118
> instantiate. The Factory method lets a class defer instantiation to subclasses.
1219
1320
For instance the `PizzaStore` abstract class in this repo provides an abstract `create_pizza` interface for creating one
1421
product.
1522

23+
### Pythonic Examples
24+
1625
The [python-qrcode](https://github.com/dancergraham/python-qrcode) module uses the factory method pattern nicely to
1726
separate only the part of the code that changes (generating png, svg, etc.) from the underlying logic of the code
1827
generation and to allow extension through the creation of new factory methods without modification of the existing code.
1928
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 🏭
2132

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
2334
> their concrete classes.
2435
2536
For instance the `PizzaIngredientFactory` abstract class defines an interface for a family of products.
2637

27-
**Builder 👷🏻‍♀️**
38+
## Builder 👷🏻‍♀️🏗️
2839

2940
When the object creation gets more complex with a number of distinct steps then the Builder pattern comes in,
3041
esseantially using a Template method to put all of the creation steps together.

0 commit comments

Comments
 (0)