Deep understanding of JavaScript: adapter mode in Design Mode

Source: Internet
Author: User

Jieshao

Adapter mode Adapter) is to convert an interface method or attribute of a Class Object) to another interface method or attribute that the customer wants ), the adapter mode makes some work for class objects that cannot work together due to interface incompatibility. Wrapper ).

Zheng Wen

Let's take an example. Duck Dock) has fly) and quack), while Turkey also has fly), but its call is gobble ). If you want to implement quack for Turkey, we can reuse the quack method, 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:

 
 
  1. // Duck
  2. Var Duck = function (){
  3.  
  4. };
  5. Duck. prototype. fly = function (){
  6. Throw new Error ("This method must be overwritten! ");
  7. };
  8. Duck. prototype. quack = function (){
  9. Throw new Error ("This method must be overwritten! ");
  10. }
  11.  
  12. // Turkey
  13. Var Turkey = function (){
  14.  
  15. };
  16. Turkey. prototype. fly = function (){
  17. Throw new Error ("This method must be overwritten! ");
  18. };
  19. Turkey. prototype. gobble = function (){
  20. Throw new Error ("This method must be overwritten! ");
  21. };

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

 
 
  1. // Duck
  2. Var MallardDuck = function (){
  3. Duck. apply (this );
  4. };
  5. MallardDuck. prototype = new Duck (); // The prototype is Duck.
  6. MallardDuck. prototype. fly = function (){
  7. Console. log ("You can fly for a long time! ");
  8. };
  9. MallardDuck. prototype. quack = function (){
  10. Console. log ("Ga! Gaga! ");
  11. };
  12.  
  13. // Turkey
  14. Var WildTurkey = function (){
  15. Turkey. apply (this );
  16. };
  17. WildTurkey. prototype = new Turkey (); // The prototype is Turkey.
  18. WildTurkey. prototype. fly = function (){
  19. Console. log ("the flight distance seems a little short! ");
  20. };
  21. WildTurkey. prototype. gobble = function (){
  22. Console. log ("giggling! Giggling! ");
  23. };

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

 
 
  1. var TurkeyAdapter = function(oTurkey){  
  2.     Duck.apply(this);  
  3.     this.oTurkey = oTurkey;  
  4. };  
  5. TurkeyAdapter.prototype = new Duck();  
  6. TurkeyAdapter.prototype.quack = function(){  
  7.     this.oTurkey.gobble();  
  8. };  
  9. TurkeyAdapter.prototype.fly = function(){  
  10.     var nFly = 0;  
  11.     var nLenFly = 5;  
  12.     for(; nFly < nLenFly;){  
  13.         this.oTurkey.fly();  
  14.         nFly = nFly + 1;  
  15.     }  
  16. }; 

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 and internally calling its own oTurkey. fly () method ).

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

 
 
  1. Var oMallardDuck = new MallardDuck ();
  2. Var oWildTurkey = new WildTurkey ();
  3. Var oTurkeyAdapter = new TurkeyAdapter (oWildTurkey );
  4.  
  5. // Original duck Behavior
  6. OMallardDuck. fly ();
  7. OMallardDuck. quack ();
  8.  
  9. // Original Turkey Behavior
  10. OWildTurkey. fly ();
  11. OWildTurkey. gobble ();
  12.  
  13. // Name of the method by which the turkey calls the duck)
  14. OTurkeyAdapter. fly ();
  15. OTurkeyAdapter. quack ();

Summary

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

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

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.