JavaScript: adapter mode in Design Mode

Source: Internet
Author: User

JavaScript: adapter mode in Design Mode
Introduction

The Adapter mode converts an interface (method or attribute) of a class (object) to another interface (method or attribute) that the customer wants ), the adapter mode makes it possible for classes (objects) that cannot work together due to interface incompatibility. Wrapper ).

Body

Let's take an example. The duck (Dock) has the fly and quack actions, while the Turkey also has the fly action, but its call is gobble ). If you want Turkey to implement quack, We can reuse the quack method of the duck, but it should be cool, we can create a turkey adapter so that the Turkey can also support the quack method, and it still needs to call gobble internally.

OK. Let's start to implement it step by step. First, we need to define the abstract behavior of duck and Turkey, that is, their respective method functions:

// Duck var Duck = function () {}; Duck. prototype. fly = function () {throw new Error (this method must be overwritten !);}; Duck. prototype. quack = function () {throw new Error (this method must be overwritten !);} // Turkey var Turkey = function () {}; Turkey. prototype. fly = function () {throw new Error (this method must be overwritten !);}; Turkey. prototype. gobble = function () {throw new Error (this method must be overwritten !);};

Then define the constructor of the specific duck and Turkey, respectively:

// Duck var MallardDuck = function () {Duck. apply (this) ;}; MallardDuck. prototype = new Duck (); // The prototype is DuckMallardDuck. prototype. fly = function () {console. log (a long distance to fly !);}; MallardDuck. prototype. quack = function () {console. log (Ga! Gaga !);}; // Turkey var WildTurkey = function () {Turkey. apply (this) ;}; WildTurkey. prototype = new Turkey (); // The prototype is TurkeyWildTurkey. prototype. fly = function () {console. log (the flight distance seems a little short !);}; WildTurkey. prototype. gobble = function () {console. log (giggling! Giggling !);};

To enable Turkey to support the quack method, we have created a new Turkey adapter TurkeyAdapter:

var TurkeyAdapter = function(oTurkey){    Duck.apply(this);    this.oTurkey = oTurkey;};TurkeyAdapter.prototype = new Duck();TurkeyAdapter.prototype.quack = function(){    this.oTurkey.gobble();};TurkeyAdapter.prototype.fly = function(){    var nFly = 0;    var nLenFly = 5;    for(; nFly < nLenFly;){        this.oTurkey.fly();        nFly = nFly + 1;    }};

This constructor accepts a turkey instance object and then applies it using Duck. Its adapter prototype is Duck, and then the quack method of its prototype needs to be modified to internally call oTurkey. gobble () method. The fly method has also made some changes, allowing the turkey to fly for five consecutive times (internally, it also calls its own oTurkey. fly () method ).

The call method is very clear. You can know the result after testing:

Var oMallardDuck = new MallardDuck (); var oWildTurkey = new WildTurkey (); var oTurkeyAdapter = new TurkeyAdapter (oWildTurkey); // The original duck behavior oMallardDuck. fly (); oMallardDuck. quack (); // The original Turkey behavior oWildTurkey. fly (); oWildTurkey. gobble (); // the behavior of the adapter Turkey (the method name for the turkey to call the duck) oTurkeyAdapter. fly (); oTurkeyAdapter. quack ();
Summary

Is the adapter mode suitable? If the following conditions occur, we recommend that you:

  1. Use an existing object, but its method or attribute interface does not meet your requirements;
  2. You want to create a reusable object that can work with other unrelated objects or invisible objects (I .e. objects with incompatible interface methods or attributes;
  3. If you want to use an existing object, you cannot perform prototype inheritance on each object to match its interface. The Object Adapter can adapt to its parent object interface methods or attributes.

    In addition, the adapter mode and several other modes may be confusing. The differences are as follows:

    1. Although the adapter and bridge mode are similar, but the starting point of the bridge is different, the purpose of the bridge is to separate the Interface part and the implementation part, so that they can be more easily and relatively independent to change. The adapter means to change the interface of an existing object.
    2. The modifier mode enhances the functions of other objects without changing its interfaces. Therefore, the program transparency is better than that of the adapter. The result is that the modifier supports recursive combination, however, it is impossible to simply use an adapter.
    3. The proxy mode defines a proxy for another object without changing its interface.

       

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.