Summary of Prototype 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 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.

Chapter 6 Prototype Patterns-Copy and Instance

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

Use of Prototype pattern

When using this pattern, use it in the following cases.

  1. Too many types of objects
  2. If it is difficult to create a new instance from the class
  3. If you want to separate the framework and the instance to be generated

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

Precautions when using the Prototype pattern

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

Example using Prototype

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.

Class diagram

prototype.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/prototype 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/Prototype

Summary

You learned about the Prototype pattern of creating another instance from an existing one.

Chapter 6 Impressions

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.

Finally

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

appendix

Recommended Posts

Summary of Prototype patterns introductory design patterns learned in Java language
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)
Prototype 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
Learn the design pattern "Prototype" in Python
Summary of methods often used in pandas
Summary of frequently used commands in matplotlib
Summary of various for statements in Python
Summary of library hosting pages by language
[Gang of Four] Design pattern learning --Prototype
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