In-depth understanding of the JavaScript series (39): Design mode adapter mode

Source: Internet
Author: User
Tags wrapper

The introduction of Adapter mode (Adapter) is the conversion of an interface (method or property) of a Class (object) into another interface (method or property) that the customer wants, and the adapter mode makes it possible to work on those classes (objects) that would otherwise not work together because of incompatible interfaces. Crash Wrapper (wrapper). For example, the duck (the Dock) has the act of flying (fly) and quack (quack), while the Turkey has a flying (fly) behavior, but its bark is cluck (gobble). If you're going to have a turkey to do that, then we can re-use the duck's quack method, but the specific call should also be quack, so we can create a turkey adapter so that the turkey also supports the quack method, with the inside still calling gobble. OK, let's start with a step-by-step implementation, first defining the abstract behavior of ducks and turkeys, which is the respective method functions://DuckvarDuck =function() {};D uck.prototype.fly=function(){Throw NewError ("The method must be overridden!"));};D Uck.prototype.quack=function(){Throw NewError ("The method must be overridden!"));}//TurkeyvarTurkey =function(){}; Turkey.prototype.fly=function(){    Throw NewError ("The method must be overridden!"));}; Turkey.prototype.gobble=function(){    Throw NewError ("The method must be overridden!"));}; Then define the specific duck and Turkey constructors, respectively://DuckvarMallardduck =function() {duck.apply ( This);}; Mallardduck.prototype=NewDuck ();//The prototype is Duck .MallardDuck.prototype.fly =function() {Console.log ("Can fly for a long distance!");}; MallardDuck.prototype.quack=function() {Console.log (Gaga Rattle! ");};//TurkeyvarWildturkey =function() {turkey.apply ( This);}; Wildturkey.prototype=NewTurkey ();//The prototype is Turkey .WildTurkey.prototype.fly =function() {Console.log ("The distance from the flight is very short.");}; WildTurkey.prototype.gobble=function() {Console.log (Giggle Cluck! ");}; In order for turkeys to also support the quack method, we created a new Turkey adapter Turkeyadapter:varTurkeyadapter =function(Oturkey) {duck.apply ( This);  This. Oturkey =Oturkey;}; Turkeyadapter.prototype=NewDuck (); TurkeyAdapter.prototype.quack=function(){     This. oturkey.gobble ();}; TurkeyAdapter.prototype.fly=function(){    varNfly = 0; varNlenfly = 5;  for(; Nfly <nlenfly;) { This. Oturkey.fly (); Nfly= Nfly + 1; The constructor accepts a turkey instance object, then uses duck to apply, its adapter prototype is duck, and then modifies the quack method of its prototype so that the Oturkey.gobble () method is called internally. Its fly method has also made some changes, allowing the turkeys to fly 5 times (internally also calls their own oturkey.fly () method). Call the method, it is very clear, the test can know the result:varOmallardduck =NewMallardduck ();varOwildturkey =NewWildturkey ();varOturkeyadapter =NewTurkeyadapter (owildturkey);//The original Duck behavioromallardduck.fly (); Omallardduck.quack ();//The original Turkey Actowildturkey.fly (); owildturkey.gobble ();//the behavior of the adapter Turkey (Turkey calls the method name of the duck)oturkeyadapter.fly (); Oturkeyadapter.quack (); the proper use of adapter mode is good? It is recommended to use an object that already exists, but whose method or property interface does not meet your requirements; you want to create a reusable object that can work with other unrelated or invisible objects (that is, objects that are incompatible with interface methods or properties), and you want to use an object that already exists. However, it is not possible to prototype each one to match its interface. The object adapter can adapt its parent object interface method or property. In addition, the adapter mode and several other modes may be confusing, here is the approximate difference: Although the adapter and bridging mode are similar, but the bridge's starting point is different, the purpose of bridging is to separate the interface and implementation parts, so that they can be more easily and relatively independent to change. The adapter, however, means changing the interface of an existing object. Decorator mode enhances the functionality of other objects without altering its interface, so it corresponds to the program's transparency better than the adapter, and the result is that the decorator supports recursive combinations, while purely using an adapter is not possible. The proxy mode defines a proxy for another object without changing its interface. Reference: https://github.com/tcorral/design-patterns-in-javascript/blob/master/adapter/index.htmlsynchronization and recommendation this article has been synchronized to the directory index: in-depth understanding of JavaScript series in-depth understanding of the JavaScript series, including the original, translation, reprint and other types of articles, if it is useful to you, please recommend supporting a, to the power of the uncle writing. 

In-depth understanding of the JavaScript series (39): Design mode adapter mode

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.