HeadFirstDesignPatterns_python/chapter04_factory at main ยท dancergraham/HeadFirstDesignPatterns_python

Chapter 4: Factory patterns

Simple Factory ๐Ÿšง

A class which chooses which product class to instantiate and return, based upon method parameters.

Use in Python

The Python standard library contains multiple references to factory objects, for instance namedtuple and 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.

Use in Python

The 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 - it was super simple once I understood the pattern.

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 ๐Ÿ‘ท๐Ÿปโ€โ™€๏ธ๐Ÿ—๏ธ

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

python pizza_abstract_factory.py