[design mode] JavaScript adapter mode

Source: Internet
Author: User

 [design mode] JavaScript adapter mode

Description: Adapter mode, generally for the interface to be used, does not conform to the application or the use of the system, and the need to introduce the intermediate adaptation layer class or object situation;

Scene: Like we bought a cell phone, bought back found that the charging line plug is three plugs, but home, only two plug the mouth of the socket, how to do? For convenience, there is also to be able to charge in any place can be charged, you have to buy a universal charger adapter; So that the phone can be charged in their own home, or can only be placed, or run to the place where the plug is charged;

In the actual development environment, because the old system, or the interface provided by a third-party application, does not match the interface we define, in an interface-oriented programming environment, such an old, or third-party interface cannot be used, and we use the adaptive class to inherit the class that is suitable for matching. And let the adaptation class Implement interface to introduce the old system or the interface of the third-party application;

When you use interface programming, you can use this adaptive class to indirectly invoke an interface from an old system or third-party application.

In JavaScript to implement code similar to the dynamic object-oriented language of the adapter pattern, can be used to prototype inheritance instances to implement, because it is based on interface constraints, but Javascript does not have an interface this thing, we remove the interface layer, directly implement the interface implementation class Target, simulate similar source code out;

  Source instance

1. Class and interface methods to be adapted:

function Adaptee () {

this.name = ' adaptee ';

}

Adaptee.prototype.getName = function () {

return this.name;

}

2. Common implementation class [The implementation class is provided directly because there is no interface in Javascript]

function Target () {

this.name = ' Target ';

}

Target.prototype.queryname= function () {

return this.name;

}

3. Adaptation class:

function Adapte () {

THIS.name = ";

}

Adapte.prototype = new Adaptee ();

Adapte.prototype.queryName = function () {

This.getname ();

}

4 How to use:

var local = new Target ();

Local.queryname (); Calling a common implementation class

var adapte = new Adapte ();

Adapte.queryname (); Call the old system or third-party application interface;

Other Notes

In the fourth step above, Var local and Var adapte are similar to the interface reference designations in object-oriented languages like java,c#, such as:

Copy Code

Interface Target {

Public String queryname ();

}

Interface references point to

Target local = new Realtarget (); The Target implementation class for the above Javascript

Local.queryname ();

Adapter

Target adapte = new Adapte ();

Adapte.queryname ();

The visible adapter class is the middle tier of the interface between the interface and the target class, which is used to solve the needs of the target already exists, but we can not directly use, and can not be used in conjunction with our code definition, we have to use the adapter mode, adaptor mode is called conversion mode, packaging mode.

[design mode] JavaScript adapter mode

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.