introduce
Adapter mode (Adapter) is to convert an interface (method or attribute) of a class (object) into another interface (method or attribute) that the customer wants. Adapter mode allows those classes (objects) that were originally unable to work together due to incompatibility of the interface. Quick wrapper.
text
Let’s give an example. A duck (Dock) has the behavior of flying (Fly) and quacking (Quack), while a turkey also has the behavior of flying (Fly), its sound is gobble. If you insist on implementing the action of quack (quack) of turkey, then we can reuse the duck's quack method, but the specific call should be quack. At this time, we can create a turkey adapter so that turkey also supports quack method, and gobble should still be called internally.
OK, we start to implement it step by step. First, we must define the abstract behavior of duck and turkey, that is, their respective method functions:
The code copy is as follows:
//Duck
var Duck = function(){
};
Duck.prototype.fly = function(){
throw new Error("This method must be rewritten!");
};
Duck.prototype.quack = function(){
throw new Error("This method must be rewritten!");
}
//turkey
var Turkey = function(){
};
Turkey.prototype.fly = function(){
throw new Error("This method must be rewritten!");
};
Turkey.prototype.gobble = function(){
throw new Error("This method must be rewritten!");
};
Then define the specific constructors of duck and turkey, respectively:
The code copy is 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 for a long distance!");
};
MallardDuck.prototype.quack = function () {
console.log("Gaga!Gaga!");
};
//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 to be a little short!");
};
WildTurkey.prototype.gobble = function () {
console.log("Gee!Gee!");
};
To make turkey support the quack method, we created a new turkey adapter, TurkeyAdapter:
The code copy is 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 takes an instance object of turkey, and then applies it using Duck, whose adapter prototype is Duck, and then it wants to re-modify its prototype's quack method so that the oTurkey.gobble() method is called internally. The fly method has also made some changes, allowing the turkey to fly 5 times in a row (the internal call is also called its own oTurkey.fly() method).
Calling the method is very clear. After testing, you will know the result:
The code copy is as follows:
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();
//Adapter turkey behavior (turkey calls duck method name)
oTurkeyAdapter.fly();
oTurkeyAdapter.quack();
Summarize
So what if you use the adapter mode? It is recommended to use if:
1. Use an existing object, but its method or attribute interface does not meet your requirements;
2. You want to create a reusable object that can work with other unrelated objects or invisible objects (i.e. objects that are incompatible with interface methods or attributes);
3. Want to use an existing object, but you cannot prototype each to match its interface. An object adapter can adapt to its parent object interface method or property.
In addition, adapter mode and several other modes may be confusing. Here is a general difference:
1. Although the adapter and bridge mode are similar, the starting point of the bridge is different. The purpose of bridge is to separate the interface part and the implementation part, so that they can be changed more easily and relatively independently. The adapter means changing an interface with an existing object.
2. The decorator mode enhances the functions of other objects without changing its interface, so its corresponding program is better transparent than the adapter. The result is that the decorator supports recursive combinations, and it is impossible to use the adapter purely.
3. The proxy mode defines a proxy for another object without changing its interface.