[PYTHON] Summary of Chapter 3 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. 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 "Adapter pattern". The previous article is below. https://qiita.com/sirajirasajiki/items/0d58a3b9fe9bdb460d0d

This time, I would like to describe the "Template Method pattern". 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.

Chapter 3 Template Method-Leave specific processing to subclasses

Do you know the genre of light novel incarnations? The genre of reincarnated things basically has a fixed flow of the main character dying → getting a cheat from God → reincarnating in another world → making a big success in another world. I say a template like this that can be used as a template. However, even if you use the template, the reason why the hero dies, the cheat received from God, the world view of the reincarnation destination, the content of activity in reincarnation, etc. will differ depending on the novel. The basic idea and the flow of the story do not change, but the person who actually makes it decides what to do in each scene.

In this way, a method that inherits the class that determines the processing framework, creates a subclass, and determines the actual processing within the subclass is called the "Template Method pattern".

Why do you need such a pattern?

If you make a lot of different classes that process with the same algorithm by copy and paste without using the "Template Method pattern", or if you find a bug in the algorithm that processes the same, copy and paste You need to modify all the created classes. If you use the "Template Method pattern", you can modify all inherited classes by modifying only the superclass.

As a concrete example, there was also "Introduction to Design Patterns Learned in Java Language",

"Display characters and strings 5 times repeatedly"

I would like to think using the content. However, add headers and footers before and after displaying characters and strings. The method to add a header is "open", the method to add a footer is "close", the method to display a character or character string is "print_string", and the method to display a character or character string 5 times is "display". will do. In the superclass, it is assumed that the three methods "open", "close", and "print_string" are abstract methods, and only "display" is implemented. It is assumed that the "display" method implemented in the superclass uses three methods, "open", "close", and "print_string".

At one point, suppose you want to change the part that "displays a character or character string 5 times repeatedly" to "display a character or character string 3 times repeatedly". If you create multiple classes by copying and pasting the processing part of the superclass without inheriting the superclass, you need to modify it in all the classes. However, if you have created a class by inheriting the superclass, you can easily fix it by changing only the superclass.

In this way, when creating multiple processes of the same algorithm, by using the "Template Method pattern", even if the process of the common part changes, it can be easily dealt with.

As mentioned in "Introduction to Design Patterns Learned in Java Language",

It is important to shape the flow of processing at the abstract class stage.

I thought again that.

Precautions when using "Template Method pattern"

Subclasses created using the "Template Method pattern" use the algorithms set in the template superclass as they are. Therefore, it is necessary to create a subclass after understanding what the superclass does.

Template Method example

Create a class diagram and Python code using the example in "Introduction to Design Patterns Learned in the Java Language". The method to add a header is "open", the method to add a footer is "close", the method to display a character or character string is "print_string", and the method to display a character or character string 5 times is "display". will do. In the superclass, it is assumed that the three methods "open", "close", and "print_string" are abstract methods, and only "display" is implemented. It is assumed that the "display" method implemented in the superclass uses three methods, "open", "close", and "print_string".

Class diagram

template_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 and use it. template_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/TemplateMethod

Summary

I learned about the "Template Method pattern" that defines the processing framework in the superclass and describes the specific processing in the subclass.

Chapter 3 Impressions

By adding specific processing at the stage of the superclass, it is possible to decide to some extent how to create it in the subclass, so even when it is implemented by someone other than the person who created the superclass, it can be implemented without much hesitation. I thought it was. Later, I couldn't think of an example this time, so I'll try to know more.

~~ The example used in the explanation will be implemented in Python at a later date. ~~ Implemented in Python.

Finally

If you find something wrong, I would appreciate it if you could point it out!

appendix

Next article

https://qiita.com/sirajirasajiki/items/3a779d3529fbc14af801

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

What I referred to when implementing in Python

How to avoid line breaks in standard output in Python https://qiita.com/tortuepin/items/4fbf29e42f05cb4b02a5

Change log

2020/3/8 Added example, class diagram, implementation in Python

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
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
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"
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 4 Step 15 Memo "Data Collection"
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]
Summary of how to use pandas.DataFrame.loc
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
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)