Adapter Mode description
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 '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.queryNamefunction() { returnthis. Name;}
3. Adaptation class:
function Adapte () { this. Name= 'newfunction()} { This . GetName ();}
4 How to use:
var New // Call the generic implementation class varnew// call the old system or the 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:
Interface Target { public String queryname ();} // interface references Point to New // the Target implementation class for the above Javascript local.queryname (); // Adapter 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, can not be used in conjunction with our code definition, we have to use the adapter mode, adaptor mode is also called conversion mode, packaging mode;
[design mode] JavaScript adapter mode