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.
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".
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.
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.
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".
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.
The code implemented below is available. Implemented in Python 3.7. https://github.com/sirajirasajiki/design_pattern_python/tree/master/TemplateMethod
I learned about the "Template Method pattern" that defines the processing framework in the superclass and describes the specific processing in the subclass.
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.
If you find something wrong, I would appreciate it if you could point it out!
appendix
https://qiita.com/sirajirasajiki/items/3a779d3529fbc14af801
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
How to avoid line breaks in standard output in Python https://qiita.com/tortuepin/items/4fbf29e42f05cb4b02a5
2020/3/8 Added example, class diagram, implementation in Python
Recommended Posts