Summary of Singleton patterns introductory 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 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.

Chapter 5 Singleton Pattern-Only One Instance

"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.

Example using Singleton

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.

Class diagram

singleton.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. 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.

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/Singleton

Precautions when using the Singleton pattern

--Keep the constructor private. --Reason: To prevent inheritance elsewhere.

Summary

You learned about the Singleton pattern, which allows you to create only one instance programmatically.

Chapter 5 Impressions

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.

Finally

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

appendix

Next article

https://qiita.com/sirajirasajiki/items/02bde7075f8edd3570f1

Articles referenced during Python implementation

https://qiita.com/ttsubo/items/c4af71ceba15b5b213f8

change history

--Addition of explanation

Recommended Posts

Summary of Singleton patterns introductory design patterns learned in Java language
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
[Updated from time to time] Summary of design patterns in Java
Design patterns learned with Java & PHP (summary)
Singleton pattern in Java
Design Patterns in Python: Introduction
Summary of various operations in Tensorflow
Chain of Responsibility pattern in Java
GoF java design pattern rough summary
Summary of multi-process processing of script language
[Gang of Four] Design pattern learning --Singleton
Summary of frequently used commands in matplotlib
Summary of various for statements in Python
Summary of library hosting pages by language
Gang of Four (GoF) Patterns in Python
Summary of stumbling blocks in installing CaboCha
Separation of design and data in matplotlib
Summary of modules and classes in Python-TensorFlow2-
Summary of built-in methods in Python list