Application analysis of Adapter adapter pattern in JavaScript design pattern Programming _ Basics

Source: Internet
Author: User
Tags urlencode wrapper

Defined
Adapter Mode (Adapter) is the transformation of an interface (a method or a property) of a Class (object) into another interface (method or property) that the customer wishes, and the adapter pattern makes it possible for those classes (objects) that would otherwise not work together because of incompatible interfaces to work. Express Wrapper (wrapper).

The adapter's alias is the wrapper (wrapper), which is a relatively simple pattern. There are many scenarios in the development of a program: when we try to call a module or an interface of an object, we find that the interface is not in the same format as the current requirement. At this time there are two solutions, the first is to modify the original interface implementation, but if the original module is very complex, or we get the module is a section of others to write the compressed code, modify the original interface is not too realistic. The second option is to create an adapter that converts the original interface to another interface that the customer wants, and the customer only needs to deal with the adapter.

Why do I need to use adapter mode?
when developing an application, you will often need to replace one of these parts, for example, a library that you use to save logs or similar content. When you replace it with a new library, the new library is unlikely to have exactly the same interface. From here, you have two choices:
(1) Check all the code and change all the code that points 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 rather than add a new layer of abstraction to make the code more complex. However, in most cases, creating an adapter is more practical and time saving.

JavaScript code example

When something can happen, it will happen. First let's take a look at this little loggerfactory, which makes it easier for us to modify the log interface we use.

var loggerfactory = {
  getlogger:function () {return
    window.console;
  },
  ...
};

/* Usage Example * *
var logger = Loggerfactory.getlogger ();
Logger.log ("Something to log");

It returned the Console object (console) to us when we called GetLogger. For this exercise, we pretend the console object has only one method--log, and it can only receive arguments of a string type. Next, we have another log interface, which is more complicated, because 1 is implemented in JavaScript, unlike the console, which is the browser itself, and 2 it sends the logs to the server via Ajax, This also means that we're going to encode the URL data (the code doesn't specifically implement URL encoding because it has nothing to do with the adapter pattern we're talking about). Of course, it uses a different interface than 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 use jquery's AJAX requests primarily to save time and ignore things that the adapter pattern doesn't want to do. What we're going to do now is create an adapter and change the loggerfactory before it returns to this adapter instead of the console object.

var ajaxloggeradapter = {
  log:function (arg) {
    ajaxlogger.sendlog (ARG);
  }
;

/* Adjust loggerfactory *

/var loggerfactory = {
  getlogger:function () {
    //Change return value returns
    ajaxloggeradapter;< c10/>},
  ...
};

We've only made one row of existing code changes, and the entire program can use this new log interface.

Complex adapters

Log interface is a very simple example, it has only one method, 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 mapping functions are completely different, and the old interfaces may not have these parameters at all, and you must deal with them yourself. In some cases, you must delete some parameters, because the new interface does not use them at all. If the interface mapping between two objects is too difficult, we'll have to think of something else, anyway, I don't want to find and modify thousands of lines of old code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.