Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the 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. 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.

What is a design pattern in the first place?

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.

Part 1 Get used to the design pattern

Chapter 1 Iterator — Count one by one

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.

Why do you need such a 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.

Class diagram

The contents of the example are shown in the class diagram as follows. iterator.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. 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.

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

Iterator type

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

Summary

It turns out that the pattern that counts aggregates one by one is called Iterator.

Chapter 1 Impressions

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.

Finally

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

appendix

Site about PlantUML

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

The site that I referred to when creating an interface and a class that inherits that interface in Python

https://qiita.com/ukisoft/items/b7c410b96dde1922a2d0#comments https://qiita.com/baikichiz/items/7c3fdb721bb72644f638 https://qiita.com/ttsubo/items/97d7dd23e8f939c81d78

Next article

https://qiita.com/sirajirasajiki/items/0d58a3b9fe9bdb460d0d

Change log

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

Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the 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 Prototype patterns introductory design patterns learned in Java language
[Updated from time to time] Summary of design patterns in Java
Summary of Singleton patterns introductory design patterns learned in Java language
Design patterns learned with Java & PHP (summary)
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
From the introduction of pyethapp to the execution of contract
Used from the introduction of Node.js in WSL environment
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 6] Introduction to scikit-learn with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 1 Memo "Preliminary Knowledge Before Beginning Exercises"
Design Patterns in Python: Introduction
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
How to calculate the amount of calculation learned from ABC134-D
What beginners learned from the basics of variables in python
How to implement Java code in the background of RedHat (LinuxONE)
Introduction to Socket API Learned in C Language Part 1 Server Edition
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 06 Memo "Identifier"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 02 Memo "Pre-processing"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 10 Memo "Details and Improvements of Neural Networks"
Chapter 1 Introduction to Python Cut out only the good points of deep learning made from scratch
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 07 Memo "Evaluation"
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 4 Step 14 Memo "Hyperparameter Search"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 04 Memo "Feature Extraction"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 4 Step 15 Memo "Data Collection"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 08 Memo "Introduction to Neural Networks"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 2 Step 05 Memo "Features Conversion"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 11 Memo "Word Embeddings"
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]
[Introduction to Python] Thorough explanation of the character string type used in Python!
[Understanding in 3 minutes] The beginning of Linux
Learning notes from the beginning of Python 1
Omit BOM from the beginning of the string
Summary of vtkThreshold (updated from time to time)
Learning notes from the beginning of Python 2
Introduction to Scapy ① (From installation to execution of Scapy)
Introduction to Effectiveness Verification Chapter 1 in Python
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 12 Memo "Convolutional Neural Networks"
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 13 Memo "Recurrent Neural Networks"
I tried to solve the 2020 version of 100 language processing knocks [Chapter 3: Regular expressions 20 to 24]
Use the Java SDK of GoogleMapsAPI to get the result of reverse GeoCoding in Japanese.
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
I tried to solve the 2020 version of 100 language processing knocks [Chapter 1: Preparatory movement 00-04]
I tried to solve the 2020 version of 100 language processing knocks [Chapter 1: Preparatory movement 05-09]
Summary of gcc options (updated from time to time)
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 09 Memo "Identifier by Neural Network"
100 Language Processing Knock UNIX Commands Learned in Chapter 2
Summary of how to import files in Python 3
100 Language Processing Knock Regular Expressions Learned in Chapter 3
Summary of how to use MNIST in Python
Change the decimal point of logging from, to.
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
Finding the beginning of Abenomics from NT magnification 2