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 6th 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 "Singleton pattern". The previous article is below. https://qiita.com/sirajirasajiki/items/532d33fd823e52ab0d1a
This time, I would like to describe the "Prototype 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.
"Introduction to Design Patterns Learned in Java Language"
"Prototype pattern" that creates another instance from one instance
There was a description. This Prototype pattern duplicates an existing instance to create a new one.
When using this pattern, use it in the following cases.
2 indicates that it is difficult to create an instance when a new instance cannot be created from a class, such as a picture instance created by the user with a paint app or a model instance created by the user with a modeling tool. ..
--Do not write the class name in the source of the class that duplicates the instance --Reason: There is a close relationship between the instance replication class and the named class.
This time, we will consider the one described in "Introduction to Design Patterns Learned in Java Language" that encloses a character string with characters or underlines the character string with a specific character. A class that encloses characters and a class that underlines a character string with a specific character is managed by a class called Manager.
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/prototype 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/Prototype
You learned about the Prototype pattern of creating another instance from an existing one.
Personally, I wondered if the Prototype pattern would be used when doing complex calculations with several patterns. For example, suppose you have a class that does numerical calculations, and you have an instance X that inherits that class. When I want to calculate the value that instance X has in both cases A and B, it is troublesome to calculate it twice, so it is easier to copy it, so I thought this idea was convenient.
~~ I will make a good example at a later date and implement it in Python. ~~ I couldn't think of an example, so I wrote it in Python using the example in the book.
If you find something wrong, I would appreciate it if you could point it out!
appendix
Recommended Posts