[PYTHON] Chapter 4 Summary of Introduction to Design Patterns Learned in Java Language

Long time no see. It's white. The other day, senior engineers don't know the design pattern! ?? After that, I was given a "Introduction to Design Patterns Learned in Java Language" written by Hiroshi Yuki, so I decided to study. However, even if I read the book, I can't remember it, so I decided to write it as a memorandum. This is the 4th time. I will do my best so that I can finish the race. In addition, there is a sample program in "Introduction to Design Patterns Learned in Java Language", but we will omit it due to copyright reasons. Please understand.

Last time, I wrote an article about "Template Method pattern". The previous article is below. https://qiita.com/sirajirasajiki/items/53e1d2aea166190f9a6f

This time, I would like to describe the "Factory Method pattern". There is also a sample program in the "Factory Method pattern", but we will omit it for copyright reasons. Please understand.

Chapter 4 Factory Method-Leave Instance Creation to Subclasses

"Introduction to Design Patterns Learned in Java Language"

Factory Method is simply the Template Method pattern applied to instantiation.

That's right.

I think some people may find it difficult to imagine, so I will give an example. Imagine a factory. At the factory, products are created. At major chain stores such as Shimamura and Uniqlo, T-shirts are often made at factories, right? I think that the size is roughly decided for each size, such as this for S size and this for M size. However, I think that the materials and designs differ from company to company. I think that other personal computers are also made at the factory. If it is a personal computer, the memory is 16GB and it will be divided like SSD or HDD. In this way, whatever you make in the factory is a product, isn't it? If making something in the factory is a superclass, the T-shirt factory is a subclass. However, you can also create a subclass called a computer factory. By using abstracted classes, you can create completely different subclasses with a common concept.

In this way, the superclass decides how to create an instance, and by leaving the specific processing to the instance, the framework for creating an instance and the creation of an instance can be considered separately. Also, by leaving the specific processing to the instance, the superclass can create an instance-independent situation.

Precautions when using Factory Method

The subclass created by the "Factory Method pattern" can decide how to create an instance in the superclass. So you need to understand the superclass properly.

Example using Factory Method

Create a class diagram and Python code using the factory example from "Introduction to Design Patterns Learned in the Java Language". This time, we will create a factory class and a product class, and create a PC factory class and a PC class that inherit each. In the factory class, an abstract method called "createProduct" that creates a product and an abstract method called "registerProduct" that registers the created product are declared, and these two abstract classes are implemented in subclasses. In addition, there is a method that implements the flow of creating a product using "createProduct" called "create" and registering the product with "registerProduct". The PC factory class inherits the factory class, implements "registerProduct" and "createProduct", and inherits a method called "get_owners" that indicates who purchased it.

The product class only implements an abstract method called "use" that uses the created product. The personal computer class inherits the product class, implements "use", and implements a method to create its own instance and a "get_owner" method to get who to send it to. When actually implementing it, it should be possible to acquire or register the destination, corporate or individual, personal computer specifications, etc., but this time it is an example using the Factory Method, so we will not do it.

Class diagram

factory_method.png

This class diagram is described as PlantUML. The PlantUML code I wrote can be found on GitHub below, so please read the ReadMe before using it. factory_method.txt. https://github.com/sirajirasajiki/design_pattern_uml/tree/master/template_method For details on how to install and use PlantUML, see the appendix below.

Implemented in Python based on the class diagram

The code implemented below is available. Implemented in Python 3.7. https://github.com/sirajirasajiki/design_pattern_python/tree/master/FactoryMethod

Summary

You learned how to create an instance in a superclass and learned about the "Factory Method pattern" that describes specific processing in subclasses.

Chapter 4 Impressions

When using not only the Factory Method pattern but basically the pattern, I thought that I needed to understand the superclass properly. So, I will write a comment about what it means for the superclass. ~~ The example used in the explanation will be implemented in Python at a later date. ~~ Implemented in Python.

Finally

If there is something wrong, I would be grateful if you could point it out!

appendix

Next article

https://qiita.com/sirajirasajiki/items/532d33fd823e52ab0d1a

Site about PlantUML

The following sites were taken care of when installing PlantUML. https://qiita.com/kohashi/items/1d2c6e859eeac72ed926 The following sites have been taken care of when writing PlantUML. https://qiita.com/ogomr/items/0b5c4de7f38fd1482a48

Recommended Posts

Summary of Chapter 2 of Introduction to Design Patterns Learned in Java Language
Chapter 4 Summary of Introduction to Design Patterns Learned in Java Language
Summary of Chapter 3 of Introduction to Design Patterns Learned in Java Language
Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the Java language
Summary of Prototype patterns introductory design patterns learned in Java language
Summary of Singleton patterns introductory design patterns learned in Java language
[Updated from time to time] Summary of design patterns in Java
Design patterns learned with Java & PHP (summary)
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 6] Introduction to scikit-learn with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
Design Patterns in Python: Introduction
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Introduction to Socket API Learned in C Language Part 1 Server Edition
Introduction to Effectiveness Verification Chapter 1 in Python
Introduction to effectiveness verification Chapter 3 written in Python
100 Language Processing Knock UNIX Commands Learned in Chapter 2
Summary of how to import files in Python 3
100 Language Processing Knock Regular Expressions Learned in Chapter 3
Summary of how to use MNIST in Python
100 language processing knocks Morphological analysis learned in Chapter 4
Introduction to Effectiveness Verification Chapter 2 Written in Python
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
Summary of tools needed to analyze data in Python
Introduction to Python language
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Design patterns to enjoy with frequently used Java libraries --Builder patterns
Design patterns to enjoy with frequently used Java libraries --Adapter patterns
Design patterns to enjoy with frequently used Java libraries --Strategy patterns
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
Summary of how to write .proto files used in gRPC
Introduction to Socket API Learned in C Part 2 Client Edition
[Introduction to cx_Oracle] Overview of cx_Oracle
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 10 Memo "Details and Improvements of Neural Networks"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 06 Memo "Identifier"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 02 Memo "Pre-processing"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 07 Memo "Evaluation"
Introduction to Socket API Learned in C Part 3 TCP Server / Client # 1
How to implement Java code in the background of RedHat (LinuxONE)
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Design patterns to enjoy with frequently used Java libraries --Template Method patterns
I tried to make an analysis base of 5 patterns in 3 years
Design patterns to enjoy with frequently used Java libraries --Facade pattern
Introduction to Socket API Learned in C Part 4 UDP Server / Client # 1
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 4 Step 14 Memo "Hyperparameter Search"
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 04 Memo "Feature Extraction"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 08 Memo "Introduction to Neural Networks"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 05 Memo "Features Conversion"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 11 Memo "Word Embeddings"
Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
100 Language Processing Knock Chapter 1 in Python
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
Summary of how to use pyenv-virtualenv
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
Introduction of data-driven controller design method
Summary of various operations in Tensorflow
Chain of Responsibility pattern in Java
Post to slack in Go language