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.
In "Introduction to Design Patterns Learned in Java Language"
Design patterns are a useful technique for reviewing the programs you write every day from a new perspective and creating software that is easy to reuse and extend.
There was a description.
If you're reading this article, wouldn't you write code similar to writing a lot of programs? I have So I often write the code while copying and pasting partly. There are at most 23 patterns that are often used in programs like this, right? There are four people who said that they are called the Gang of Four or GoF. GoF says that "Design Patterns: Elements of Reusable Object-Oriented Software" is a book that names 23 patterns and organizes them into a catalog. Below are links that mention the book in question. https://en.wikipedia.org/wiki/Design_Patterns
I wrote it in a mess, but I think it's about trying to learn the pattern of how to write a program in order to write a program that is easy to reuse and expand.
There are programs that repeat processing in order, such as for statements and while statements. It seems that the pattern that is repeatedly processed in this way is called the Iterator pattern.
As mentioned in "Introduction to Design Patterns Learned in Java Language", I think there is an opinion that for statements should be used for things that are processed using arrays. The bottom line is that if you abstract things that are executed in sequence, you don't have to change the methods that are executed in sequence even if the data structure to be executed changes.
Below is an example I have come up with. However, the Iterator interface and Aggregate interface use those described in "Introduction to Design Patterns Learned in the Java Language".
For example, suppose you manage a student at a university.
Suppose you have an Iterator interface that declares a "method to find next" and a "method to get the next object". We also assume that you have an Aggregate interface that declares an interface for this Iterator.
Suppose you have multiple students in a department, and each student has a name and student ID number. Suppose you have a student class that manages student student ID numbers and names, and you have a department class that manages students who belong to a department in a List format. The department class shall implement the Aggregate interface. In the department class, "method to get students in List type variable", "method to get how many students are", "method to add students", and "method to add students" described later. Suppose you have a method that returns an instance of the Iterator class.
Suppose you have a Department Iterator class that takes students in a department in order of student ID number. This department Iterator class implements the Iterator interface. In this department Iterator class, there is an integer type variable a that manages how far you have searched, "a method to determine whether there is a student with the next student ID number", and "get a student and variable a". Suppose you have a method that adds 1 to the value of. In order to determine whether this department Iterator class still has students, the value of the "method that can obtain how many students there are" that the department class has is compared with the variable a of the department Iterator class, and the variable a Suppose you still have students when the value is smaller. In addition, when the department Iterator class acquires students, it shall use the "method for acquiring students contained in List type variables" of the department class.
At this time, even if the student management method is changed from List type to ArrayList type in the department class, the implementation of the department Iterator class is not affected, so the range of influence is limited to the department class. Iterator design is needed at such times.
"Introduction to Design Patterns Learned in Java Language"
Keep in mind the idea of programming with abstract classes and interfaces.
There was a description, so don't forget it.
The contents of the example are shown in the class diagram as follows.
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. The corresponding file name is iterator.txt. https://github.com/sirajirasajiki/design_pattern_uml/blob/master/iterator/iterator.txt
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/Iterator
The ReadMe can be found on the following page. https://github.com/sirajirasajiki/design_pattern_python/blob/master/README.md
The implementation of classes and interfaces in Python is described in the appendix below.
In addition to processing in order from the beginning, there are the following types of processing that are repeated in order. --Reverse order --Bidirectional --Process from the middle to the end
It turns out that the pattern that counts aggregates one by one is called Iterator.
By abstracting even simple processing, I thought it would be easy because there would be few changes. Also, I tried to express the class in Japanese, but it was a relatively simple concept, but it was very difficult, so I thought the program was great. After that, it was very difficult to create a class diagram while thinking about it, so I would like to write it on paper. ~~ I will write it like that in Python at a later date. ~~ I wrote it like that in Python.
If there is something wrong, I would be grateful if you could point it out!
appendix
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
https://qiita.com/ukisoft/items/b7c410b96dde1922a2d0#comments https://qiita.com/baikichiz/items/7c3fdb721bb72644f638 https://qiita.com/ttsubo/items/97d7dd23e8f939c81d78
https://qiita.com/sirajirasajiki/items/0d58a3b9fe9bdb460d0d
2020/2/22 Added class diagram items 2020/2/24 Implemented in Python based on the class diagram, and modified the class diagram according to the Python method naming convention. 2020/2/25 Addition of appendix. 2020/2/27 Change the Git link in the class diagram
Recommended Posts