Deep understanding of the JavaScript series (39): The design pattern of the adapter pattern Detailed _ Basics

Source: Internet
Author: User
Tags wrapper

Introduced

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).

Body

Let's cite an example, the duck (Dock) has the behavior of flying (fly) and quack (quack), while the Turkey also has the act of flying (fly), but its sound is cluck (gobble). And if you want a turkey, you're going to have to do quack-quack. Then we can reuse the duck quack method, but the specific call should also be giggle, at this time, we can create a turkey adapter, so that the Turkey also supports the quack method, its internal or to call gobble.

OK, let's start with a step-by-step approach, first defining the abstract behavior of ducks and turkeys, which is the respective method function:

Copy Code code as follows:

Duck
var Duck = function () {

};
Duck.prototype.fly = function () {
throw new Error ("This method must be overridden!");
};
Duck.prototype.quack = function () {
throw new Error ("This method must be overridden!");
}

Turkey
var Turkey = function () {

};
Turkey.prototype.fly = function () {
throw new Error ("This method must be overridden!");
};
Turkey.prototype.gobble = function () {
throw new Error ("This method must be overridden!");
};


Then define the specific duck and Turkey constructors, respectively:
Copy Code code as follows:

Duck
var mallardduck = function () {
Duck.apply (this);
};
Mallardduck.prototype = new Duck (); The prototype is duck.
MallardDuck.prototype.fly = function () {
Console.log ("Can fly a long distance!");
};
MallardDuck.prototype.quack = function () {
Console.log ("Quack!") Rattle! ");
};

Turkey
var wildturkey = function () {
Turkey.apply (this);
};
Wildturkey.prototype = new Turkey (); The prototype is Turkey.
WildTurkey.prototype.fly = function () {
Console.log ("The flying distance seems a little short!");
};
WildTurkey.prototype.gobble = function () {
Console.log ("Giggle!") Cluck! ");
};


To allow the Turkey to also support the quack method, we created a new Turkey adapter Turkeyadapter:
Copy Code code as follows:

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;
}
};

The constructor accepts an instance object of a turkey and then uses duck to apply, its adapter prototype is duck, and then modifies its prototype's quack method so that the Oturkey.gobble () method is called internally. The Fly method has also made some changes to allow the turkey to fly 5 consecutive times (internal is also called its own Oturkey.fly () method).

Call the method, it is clear, the test will be able to know the results:

Copy Code code as follows:

var omallardduck = new Mallardduck ();
var owildturkey = new Wildturkey ();
var oturkeyadapter = new Turkeyadapter (Owildturkey);

The original Duck behavior
Omallardduck.fly ();
Omallardduck.quack ();

The original Turkey Act
Owildturkey.fly ();
Owildturkey.gobble ();

The behavior of the adapter Turkey (Turkey calls the method name of the duck)
Oturkeyadapter.fly ();
Oturkeyadapter.quack ();

Summarize

Is it appropriate to use adapter mode? It is recommended that you use the following conditions:

1. Use an existing object, but its method or property interface does not meet your requirements;
2. You want to create a reusable object that can work in conjunction with other unrelated objects or objects that are not visible (that is, interface methods or attributes incompatible);
3. You want to use an object that already exists, but you cannot inherit each one to match its interface. An object adapter can fit its parent object interface method or property.

In addition, the adapter pattern and several other patterns may be confusing, and here's the general difference:

1. While adapters and bridging modes are similar, bridging starts with a different starting point, the purpose of which is to separate the part of the interface from the implementation, so that they can be changed more easily and relatively independently. The adapter, then, means changing the interface of an existing object.
2. The adorner pattern enhances the functionality of other objects without altering its interface, so it is better to have the transparency of the program than the adapter, and the result is that the adorner supports the recursive combination, and the sheer use of the adapter is impossible.
3. The proxy mode defines an agent for another object without changing its interface.

Related Article

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.