Adapter mode description
Note: The adapter mode is generally the case where the interface to be used is not consistent with the application or the system, but needs to be introduced;
Scene: It’s like we bought a mobile phone, and after buying it, we found that the charging cable plug is three plugs, but at home, there are only two plugs in the socket. What should we do? For convenience, and to be able to charge anywhere, you have to buy a universal charging adapter; so that the phone can be charged at home; otherwise you can only place it, or run to a place with this plug to charge;
In the actual development environment, since the old system or interface provided by third-party applications does not match the interface defined, such old or third-party interfaces cannot be used in an interface-oriented environment. At this time, we use the adapter class to inherit the classes to be adapted and let the adapter class implement interfaces to introduce the interfaces of the old system or third-party applications;
In this way, when using interface programming, you can use this adaptive matching class to indirectly call the interface of the old system or third-party application.
In Javascript, to implement code similar to the adapter pattern of dynamic object-oriented language, you can use inherited instances of prototype to implement it; because it is based on interface constraints, but Javascript does not have an interface number, we remove the interface layer and directly implement the interface implementation class Target to simulate similar source code;
Source code example
1. Classes and interface methods to be adapted:
The code copy is as follows:
function Adaptee() {
this.name = 'Adaptee';
}
Adaptee.prototype.getName = function() {
return this.name;
}
2. Normal implementation class [Since there is no interface in Javascript, the implementation class is directly provided]
The code copy is as follows:
function Target() {
this.name = 'Target';
}
Target.prototype.queryName= function() {
return this.name;
}
3. Adaptation class:
The code copy is as follows:
function Adapte() {
this.name = '';
}
Adapte.prototype = new Adaptee();
Adapte.prototype.queryName = function() {
this.getName();
}
4. How to use:
The code copy is as follows:
var local = new Target();
local.queryName(); //Calling the normal implementation class
var adapte = new Adapte();
adapte.queryName(); //Calling the old system or third-party application interface;
Other Instructions
In the fourth step above, var local and var adapte are similar to object-oriented languages such as Java and C#, such as:
The code copy is as follows:
interface Target {
public String queryName();
}
//Interface reference pointer
Target local = new RealTarget(); //That is, the Target implementation class of Javascript above
local.queryName();
//adapter
Target adapte = new Adapte();
adapte.queryName();
It can be seen that the adapter class is the intermediate layer connecting the interface to the target class interface; it is used to solve the problem that the required target already exists, but we cannot use it directly and cannot use it in conjunction with our code definition, so we have to use the adapter mode. The adapter mode is also called the conversion mode and the packaging mode;