definition
Adapter mode (Adapter) is to convert an interface (method or attribute) of a class (object) into another interface (method or attribute) that the customer wants. Adapter mode allows those classes (objects) that were originally unable to work together due to incompatibility of the interface. Quick wrapper.
The alias for the adapter is a wrapper, which is a relatively simple pattern. There are many such scenarios in program development: when we try to call an interface of a module or object, we find that the format of this interface does not meet the current requirements. There are two solutions at this time. The first is to modify the original interface implementation, but if the original module is very complicated, or the module we get is a compressed code written by someone else, modifying the original interface will be unrealistic. The second method is to create an adapter and convert the original interface into another interface the customer wants. The customer only needs to deal with the adapter.
Why do you need to use adapter mode?
When developing an application, you often need to replace a part of it, for example, a library you use to hold logs or similar content. When you replace it with a new library, it is unlikely that the new library has the exact same interface. From here, you have two options:
(1) Check all code and change all code pointing to the old library.
(2) Create an adapter so that the new library can use the same interface as the old library.
Obviously, in some cases, if your application is small, or there are few references to the old library, it is more appropriate to check the complete code and change it to match the new library instead of adding a new abstraction layer to make the code more complicated. However, in most cases, creating an adapter is more practical and time-saving.
JavaScript code examples
When something can happen, it will definitely happen. First let's take a look at this little LoggerFactory, which allows us to modify the log interface we use more easily.
var LoggerFactory = { getLogger: function() { return window.console; }, ...};/* Usage example*/var logger = LoggerFactory.getLogger();logger.log("something to log");When we call getLogger it returns us the console object (console). For this exercise, we pretend that the console object has only one method - log, and it can only receive one string type parameter. Next, we have another log interface, which will be more complicated because 1) it is implemented in JavaScript, unlike console, which is not the browser itself; 2) it will send the logs to the server through AJAX, which also means that we have to encode the URL data (the code will not specifically implement URL encoding related matters, because it has nothing to do with the adapter pattern we want to talk about). Of course, it will use a different interface from the console.
var AjaxLogger = { sendLog: function() { var data = this.urlEncode(arguments); jQuery.ajax({ url: "http://example.com/log", data: data }); }, urlEncode: function(arg) { ... return encodedData; }, ...};We used jQuery's AJAX requests, mainly to save time and ignore things that we don't want to do with the adapter mode. What we have to do now is create an adapter and change the previous LoggerFactory to return this adapter instead of the console object.
var AjaxLoggerAdapter = { log: function(arg) { AjaxLogger.sendLog(arg); }};/* Adjust LoggerFactory */var LoggerFactory = { getLogger: function() { // Change the return value return AjaxLoggerAdapter; }, ...};We only made one line of changes to the existing code, and the entire program can use this new log interface.
Complex adapter
The log interface is a very simple example. It only has one method, and it is not difficult to map it directly to the old method. This is not the case in most cases. You may encounter the problem that the parameters of these mapped functions are completely different, and the old interface may not have these parameters at all, and you have to deal with them yourself. In some cases, you have to delete some parameters because new interfaces don't use them at all. If the interface mapping between two objects is too difficult, we have to think of other ways, anyway, I don't want to find and modify thousands of lines of old code.