“This is the 29th day of my participation in the August Gwen Challenge.

preface

At the advanced end of the interview, there are often some questions about design patterns, and the answers are not good. Coincides with the activities of more challenges in August, I plan to spend a month to clarify the knowledge points about design patterns, to increase the confidence of my interview.

define

The adapter pattern solves the problem of method incompatibilities between two classes. With the adapter pattern, two classes that would otherwise not work due to method incompatibility can work together.

The adapter pattern is a relatively simple one. There are many scenarios in program development where we try to call a method on a module or object, only to find that the method’s format does not meet our current requirements.

There are two solutions. The first is to modify the original method implementation, but if the original module is complex, or if the module is a piece of compressed code written by someone else, modifying the original method is not practical. The second option is to create an adapter that transforms the original method into another method that the customer wants, and the customer only needs to deal with the adapter.

understand

Take a life example to understand, for example, the battery of the notebook is 20V, and the AC voltage in daily life is generally 220V. With the exception of 220V AC, the ac voltage in Japan and South Korea is mostly 100V, while that in the UK and Australia is 240V. The power adapter of the notebook computer assumes the role of voltage conversion. The power adapter enables the notebook computer to work normally within 100V~240V voltage, which is why it is called the power adapter.

application

If the existing approach had worked, we would never have used the adapter pattern. The adapter pattern is a “never too late” pattern that no one will use at the beginning of a program’s design. Since no one can fully predict what will happen in the future, it may be that the methods that work well now will one day not be suitable for the new system, so we can use the adapter pattern to wrap the old method into a new method and keep it alive.

If there is a need, switch tabs to show different maps

Const googleMap = {show: function () {console.log(' Start rendering Google Maps '); }}; Const baiduMap = {show: function () {console.log(' start rendering baiduMap '); }}; const renderMap = function (map) { if (map.show instanceof Function) { map.show(); }}; renderMap(googleMap); // Output: Start rendering Google Map renderMap(baiduMap); // Output: Start rendering Baidu mapCopy the code

The key to the successful operation of the above code is that googleMap and baiduMap provide the same show method, but the method provided by the third party is not within our control. What if baiduMap provides a method to display maps that is called display instead of show? This is where the adapter pattern comes in.

The baiduMap object comes from a third party, and we should not change it under normal circumstances. You can solve the problem by adding a baiduMapAdapter:

Const googleMap = {show: function () {console.log(' Start rendering Google Maps '); }}; Const baiduMap = {display: function () {console.log(' start rendering baiduMap '); }}; const baiduMapAdapter = { show: function () { return baiduMap.display(); }}; renderMap(googleMap); // Output: Start rendering Google Map renderMap(baiduMapAdapter); // Output: Start rendering Baidu mapCopy the code

The above baiduMapAdapter is the adapter of baiduMap.

Differences from decorator, proxy, and facade patterns

  • The adapter pattern resolves the mismatch between two existing methods, regardless of how those methods were implemented or how they might evolve in the future. The adapter pattern enables them to work together without changing existing methods.

  • Decorator mode and proxy mode also do not change the method of the original object, but the purpose of decorator mode is to add functionality to the object. The decorator pattern often forms a long chain of decorators, whereas the adapter pattern usually wraps once. The proxy pattern is designed to control access to objects and is usually wrapped only once.

  • A facade pattern is similar to an adapter. Some people think of the facade pattern as an adapter for a set of objects, but the most notable feature of the facade pattern is that it defines a new method.