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.
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.
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.
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.
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.
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.
The code implemented below is available. Implemented in Python 3.7. https://github.com/sirajirasajiki/design_pattern_python/tree/master/Adapter/class_case
The difference from the Adapter pattern depending on the class is that GetTime is used differently in GetTimeStr.
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
The code implemented below is available. Implemented in Python 3.7. https://github.com/sirajirasajiki/design_pattern_python/tree/master/Adapter/instance_case
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.
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.
If there is something wrong, I would be grateful if you could point it out! appendix
https://qiita.com/sirajirasajiki/items/53e1d2aea166190f9a6f
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
2020/2/28 Partially updated impressions and examples. 2020/3/5 The beginning part was corrected.
Recommended Posts