Learn the design pattern "Factory Method" in Python

As a material for learning GoF design patterns, the book "Introduction to Design Patterns Learned in the Augmented and Revised Java Language" seems to be helpful. However, since the examples taken up are based on JAVA, I tried the same practice in Python to deepen my understanding.

■ Factory Method

The Factory Method pattern is one of the design patterns defined by GoF (Gang of Four). The Factory Method pattern aims to expel application-specific object creation to subclasses and improve class reusability by replacing constructors of other classes with their own methods that can be overridden by subclasses. Also known as the Virtual Constructor pattern. UML class diagram W3sDesign_Factory_Method_Design_Pattern_UML.jpg 1000px-Factory_Method_UML_class_diagram.svg.png (The above is quoted from Wikipedia)

□ Memorandum

The Template Method pattern for behavior is to create a general processing outline in a superclass and determine specific processing in a subclass. It seems that the Factory Method pattern is the one that applies this Template Method pattern to the scene of creating an instance.

■ "Factory Method" sample program

The Factory Method pattern seems to have the following features.

--Determine how to create an instance on the superclass side --No specific class name is specified --All specific fleshing is done on the subclass side

By doing this, it seems that it will be possible to consider the framework for instance creation and the actual instance creation class separately. Actually, I would like to use the Factory Method pattern to run a sample program with the theme of "a factory that makes ID cards (ID cards)" and check the following operation.

--Create three ID cards: Hiroshi Yuki" , Tomura, Hanako Sato --Use 3 ID cards: Hiroshi Yuki" , Tomura, Hanako Sato

$ python Main.py 
I'll create Hiroshi Yuki's card
I'll create Tomura's card
I'll create Hanako Sato's card
I'll use Hiroshi Yuki's card
I'll use Tomura's card
I'll use Hanako Sato's card

If you just run the sample program, you don't really know what you want to do. Next, let's check the details of the sample program.

■ Details of sample program

Similar code has been uploaded to the Git repository. https://github.com/ttsubo/study_of_design_pattern/tree/master/FactoryMethod

--Directory structure

.
├── Main.py
└── framework
    ├── __init__.py
    ├── factory.py
    └── idcardfactory
        ├── __init__.py
        └── id_card_factory.py

(1) Role of Product

The Product role defines the interface that an instance should have. In the sample program, the Product class serves this role.

framework/factory.py


from abc import ABCMeta, abstractmethod

... (snip)

class Product(metaclass=ABCMeta):
    @abstractmethod
    def use(self):
        pass

(2) The role of Creator

In the role of Creator, it is responsible for generating Product. I don't know anything about the Concrete Product role that is actually generated. In addition, the Creator role provides methods for creating each part of the instance. In the sample program, the Factory class serves this role.

framework/factory.py


from abc import ABCMeta, abstractmethod

class Factory(metaclass=ABCMeta):
    def create(self, owner):
        p = self.createProduct(owner)
        self.registerProduct(p)
        return p

    @abstractmethod
    def createProduct(self, owner):
        pass

    @abstractmethod
    def registerProduct(self, product):
        pass

... (snip)

(3) Role of Concrete Product

The ConcreteProduct role is a class that implements the interface for the Product role. The method that will be called after the actual instance creation is defined here. In the sample program, the ʻIDCardProduct` class serves this role.

framework/id_card_factory.py


from framework.factory import Factory, Product

... (snip)

class IDCardProduct(Product):
    def __init__(self, owner):
        self.owner = owner
        print("I'll create {0}'s card".format(self.owner))

    def use(self):
        print("I'll use {0}'s card".format(self.owner))

(4) The role of Concrete Creator

The ConcreteCreator role is a class that implements the interface for the Creator role. Determine the class to make a specific product. In the sample program, the ʻIDCardFactory` class serves this role.

framework/id_card_factory.py


from framework.factory import Factory, Product

class IDCardFactory(Factory):
    def __init__(self):
        self.owners = []

    def createProduct(self, owner):
        return IDCardProduct(owner)

    def registerProduct(self, product):
        self.owners.append(product.owner)

... (snip)

(5) The role of Client

In the sample program, the startMain method serves this role.

Main.py


from framework.idcardfactory.id_card_factory import IDCardFactory

def startMain(factoryObject):
    card1 = factoryObject.create("Hiroshi Yuki")
    card2 = factoryObject.create("Tomura")
    card3 = factoryObject.create("Hanako Sato")
    card1.use()
    card2.use()
    card3.use()

if __name__ == "__main__":
    startMain(IDCardFactory())

■ Reference URL

-[Finishing "Introduction to Design Patterns Learned in Java Language" (Not)](https://medium.com/since-i-want-to-start-blog-that-looks-like-men-do/java Introduction to Design Patterns Learned in Languages-Finishing-No-2cc9b34a30b2) -Factory Method pattern from "diary of tachikawa844"

Recommended Posts

Learn the design pattern "Factory Method" in Python
Learn the design pattern "Abstract Factory" in Python
Learn the design pattern "Template Method" in Python
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Learn the design pattern "Flyweight" in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the design pattern "Proxy" in Python
Learn the design pattern "Command" in Python
Learn the design pattern "Bridge" in Python
Learn the design pattern "Mediator" in Python
Learn the design pattern "Decorator" in Python
Learn the design pattern "Iterator" in Python
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
Learn the design pattern "State" in Python
Learn the design pattern "Adapter" in Python
Learn the design pattern "Chain of Responsibility" in Python
Design Pattern #Factory Method
Learn the design pattern "Singleton" with Python
Learn the design pattern "Facade" with Python
Python Design Pattern --Template method
Implement the Singleton pattern in Python
Singleton pattern in Python
Visitor pattern in Python
[Gang of Four] Design pattern learning --Factory Method
I tried the least squares method in Python
To dynamically replace the next method in python
Simplex method (simplex method) in Python
Design Pattern #Template Method
Private method in python
Try implementing the Monte Carlo method in Python
I wrote a design pattern in kotlin Factory edition
Determine the threshold using the P tile method in python
Implemented k-nearest neighbor method in python from scikit learn
Download the file in Python
Find the difference in Python
Learn cumulative sum in Python
Design Patterns in Python: Introduction
Template Method pattern in Java
Implement method chain in Python
Learn exploration in Python # 1 Full exploration
Suppressing method overrides in Python
How to use the __call__ method in a Python class
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Try implementing extension method in python
Get the script path in Python
In the python command python points to python3.8
Implemented label propagation method in Python
Simulate Monte Carlo method in Python
Hit the web API in Python
Hash method (open address method) in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python