Summary of Chapter 2 of Introduction to 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. 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 "Iterator pattern". The previous article is below. https://qiita.com/sirajirasajiki/items/55269e5d6c6e158de16e

This time, I would like to describe the "Adapter 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 2 Adapter-Skin Cover and Reuse

I think there are electrical appliances used in Japan. When using this electrical product overseas, the voltage is different, so I think I will bring a converter for the outlet. In this way, when the already supplied item cannot be used as it is, it can be used by converting it into a usable form. It is said that such a conversion pattern is called an Adapter pattern. "Introduction to Design Patterns Learned in Java Language"

Design patterns to bridge the "difference" between "what is already provided" and "what you need"

There was a description saying. This Adapter pattern is also called the Wrapper pattern.

Why do you need such a pattern?

There was a description in "Introduction to Design Patterns Learned in Java Language", but I think there is an opinion that if there is a necessary method, you can create it additionally. However, if you create all the methods from scratch, the test effort will increase tremendously.

For example, suppose you have a method that gets unixtime and returns it as an int, and you want a new method that returns unixtime as a string. In such a simple example, casting the return value of the method as a string type is fine, but let's assume that you have created a new method that returns the time as a string type. If an error occurs in this newly created method, you can't tell from the part that is getting the time, the part that is casting as a string type, or the part that the error occurred. ?? However, if you use a method that gets an existing unixtime and returns it as an int type and creates a new method that returns the time as a string type and an error occurs, the existing method is used for the error in the part that gets the time. I've confirmed that it's not, so it's easy to see that casting from an int to a string is failing. Also, when you create a method, you only have to call the existing method, so it's easy to implement. In this way, by using the Adapter pattern, it is possible to reduce the mounting man-hours and the causes of errors that occur during mounting.

Consider also modifying the return value of a method that gets an existing unixtime and returns it as an int so that it is cast as a string. As mentioned in "Introduction to Design Patterns Learned in the Java Language", it is better to use the Adapter pattern even in this case. The reason is that it modifies the existing implementation, so you have to test the part that uses this method again, so even a small modification will take a lot of testing effort and will be wasted.

There was a description in "Introduction to Design Patterns Learned in Java Language", but if the functions are far apart, there is no choice but to create a new class. The image is as follows.

It is not possible to produce tap water based on an AC 100 volt power supply.

Adapter pattern type

There are two types of this Adapter pattern as follows. -Adapter pattern by class (using inheritance) -Adapter pattern by instance (using delegation) I would like to describe each pattern for the example.

Example

We'll create a wrapper that returns a string type using a class that has a method that gets the unixtime of the time the instance was created and returns it as an int type.

Adapter pattern by class

Class diagram

class.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 class.txt. https://github.com/sirajirasajiki/design_pattern_uml/blob/master/adapter/class.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/Adapter/class_case

Adapter pattern by instance

Class diagram

The difference from the Adapter pattern depending on the class is that GetTime is used differently in GetTimeStr. instance.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. https://github.com/sirajirasajiki/design_pattern_uml/blob/master/adapter/instance.txt

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/Adapter/instance_case

Summary

It turned out that the Adapter pattern is the one that absorbs the deviation when the format you want to use is different from the existing class.

Chapter 2 Impressions

By creating a new class using an existing implementation, the man-hours can be reduced compared to creating a new one, and if an error occurs in the new implementation, the range of the cause of the error can be reduced, so I thought it would be easy to implement. It was. Also, even a simple modification that changes the return type will cause the test to be redone if the modification is made to the existing implementation, so I understand that this Adapter pattern should be used in such cases. It was.

Postscript I heard that it is better to make a wrapper than to make an adapter, so from now on, I will say that I will make a wrapper.

Finally

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

Next article

https://qiita.com/sirajirasajiki/items/53e1d2aea166190f9a6f

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

Change log

2020/2/28 Partially updated impressions and examples. 2020/3/5 The beginning part was corrected.

Recommended Posts

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
Summary of Prototype patterns introductory design patterns learned in Java language
Summary of Singleton patterns introductory design patterns learned in Java language
[Updated from time to time] Summary of design patterns in Java
Design patterns learned with Java & PHP (summary)
[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
Design Patterns in Python: Introduction
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Introduction to Socket API Learned in C Language Part 1 Server Edition
Introduction to Effectiveness Verification Chapter 1 in Python
Introduction to effectiveness verification Chapter 3 written in Python
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
100 language processing knocks Morphological analysis learned in Chapter 4
Introduction to Effectiveness Verification Chapter 2 Written in Python
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
Summary of tools needed to analyze data in Python
Introduction to Python language
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Design patterns to enjoy with frequently used Java libraries --Builder patterns
Design patterns to enjoy with frequently used Java libraries --Adapter patterns
Design patterns to enjoy with frequently used Java libraries --Strategy patterns
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
Summary of how to write .proto files used in gRPC
Introduction to Socket API Learned in C Part 2 Client Edition
[Introduction to cx_Oracle] Overview of cx_Oracle
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 3 Step 10 Memo "Details and Improvements of Neural Networks"
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 2 Step 07 Memo "Evaluation"
Introduction to Socket API Learned in C Part 3 TCP Server / Client # 1
How to implement Java code in the background of RedHat (LinuxONE)
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Design patterns to enjoy with frequently used Java libraries --Template Method patterns
I tried to make an analysis base of 5 patterns in 3 years
Design patterns to enjoy with frequently used Java libraries --Facade pattern
Introduction to Socket API Learned in C Part 4 UDP Server / Client # 1
Try the book "Introduction to Natural Language Processing Application Development in 15 Steps" --Chapter 4 Step 14 Memo "Hyperparameter Search"
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
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"
Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
Summary of how to use pandas.DataFrame.loc
100 Language Processing Knock Chapter 1 in Python
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
Summary of how to use pyenv-virtualenv
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
Introduction of data-driven controller design method
Summary of various operations in Tensorflow