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 5th 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 "Factory Method pattern". The previous article is below. https://qiita.com/sirajirasajiki/items/3a779d3529fbc14af801
This time, I would like to describe the "Singleton pattern". There is also a sample program for the "Singleton pattern", but we will omit it for copyright reasons. Please understand.
"Introduction to Design Patterns Learned in Java Language"
The pattern that guarantees that there is only one instance is called the Singleton pattern.
There was a description.
For example, suppose you have a program that performs multiple types of operations. For each operation, the operation result is used for the next operation. Suppose you have an operation management class that manages the results of these operations all at once. At this time, if multiple operation classes can be created, the operation result will change for each instance. Therefore, you can get the calculation result that you originally wanted by making one instance. In this way, it is used when you want to use only one instance that inherits a specific class programmatically.
A common example is the class of parameters that control a machine. For example, if there are multiple instances in the same device that manage parameters such as machine temperature and machine operating status, the device will not know which instance to refer to, right? Also, the parameters are different for each instance and you may not know which one is correct, right? Use this Singleton pattern at such times.
This time, I would like to consider the situation where Mahjong score management is performed using the Singleton pattern. If you rebuild the instance every time in the same game of Mahjong, the final result of the score will be strange, so we will use the Singleton pattern. In a class called Mahjong, points and instances are managed by private variables. In addition, it is assumed that the methods for acquiring Mahjong instances, acquiring points, and calculating points are managed.
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. It is singleton.txt. https://github.com/sirajirasajiki/design_pattern_uml/tree/master/singleton 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/Singleton
--Keep the constructor private. --Reason: To prevent inheritance elsewhere.
You learned about the Singleton pattern, which allows you to create only one instance programmatically.
By using the Singleton pattern, it was found that when multiple instances are created, even classes where the instances interfere with each other and affect the behavior of the program can be used with confidence. ~~ The example used in the explanation will be implemented in Python at a later date. ~~ Implemented in Python.
If there is something wrong, I would be grateful if you could point it out!
appendix
https://qiita.com/sirajirasajiki/items/02bde7075f8edd3570f1
https://qiita.com/ttsubo/items/c4af71ceba15b5b213f8
--Addition of explanation
Recommended Posts