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:
- // 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 Duck.
- MallardDuck. prototype. fly = function (){
- Console. log ("You can fly for a long time! ");
- };
- MallardDuck. prototype. quack = function (){
- Console. log ("Ga! Gaga! ");
- };
-
- // Turkey
- Var WildTurkey = function (){
- Turkey. apply (this );
- };
- WildTurkey. prototype = new Turkey (); // The prototype is Turkey.
- WildTurkey. 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 and internally calling 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 );
-
- // Original duck Behavior
- OMallardDuck. fly ();
- OMallardDuck. quack ();
-
- // Original Turkey Behavior
- OWildTurkey. fly ();
- OWildTurkey. gobble ();
-
- // Name of the method by which the turkey calls the duck)
- OTurkeyAdapter. fly ();
- 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: