Describes how to implement the adapter mode in the design mode in JavaScript, And the javascript design mode.
Sometimes, during the development process, we will find that the interfaces required by the client and the interfaces provided are incompatible. We cannot modify the client interface for special reasons. In this case, we need to adapt to existing interfaces and incompatible classes. This requires the adapter mode. Using adapters, we can use them without modifying the Old Code. This is the capability of the adapter.
The adaptation mode can be used to adapt between existing interfaces and incompatible classes. The objects in this mode are called wrapper ), because they are packaging another object with a new interface.
On the surface, the adapter mode is similar to the appearance mode. They all need to wrap other objects and change their rendering interfaces. The difference between the two lies in how they change interfaces. The appearance element is a simplified interface that does not provide additional options, and sometimes it makes assumptions to facilitate the completion of common tasks. The adapter converts an interface to another interface, which does not filter out certain capabilities or simplify the interface. If the customer's system API is unavailable, an adapter is required.
Basic Theory
Adapter mode: converts an interface to the interface required by the client without modifying the client code, so that incompatible code can work together.
The adapter consists of three roles:
(1) client: the class that calls the interface
(2) Adapter: A class used to connect client interfaces and interfaces that provide services
(3) Adapter: provides services, but is not compatible with client interface requirements.
Adapter mode implementation
1. The simplest Adapter
The adapter mode is not as complex as you think. Here is a simple example.
The client calls a method for addition calculation:
var result = add(1,2);
However, we didn't provide the add method, and provided the sum method with the same functions:
function sum(v1,v2){ return v1 + v2;}
To avoid modifying the client and server, we add a packaging function:
function add (v1,v2){ reutrn sum(v1,v2);}
This is the simplest adapter mode. we add a packaging method between two incompatible interfaces and use this method to connect them to work together.
2. Practical Application
With the development of front-end frameworks, more and more developers begin to use MVVM frameworks for development. They only need to operate data without the need to operate DOM elements. jQuery has fewer and fewer functions. Many projects reference the jQuery library tool class, because we need to use ajax provided by jQuery to request data on the server. If jQuery is only used as an ajax Tool library in a project, it will be a bit cool and cause a waste of resources. In this case, we can encapsulate a self-built ajax library.
Assume that the encapsulated ajax is used through a function:
ajax({ url:'/getData', type:'Post', dataType:'json', data:{ id:"123" }}).done(function(){})
Except for calling the interface ajax and jQuery's $. ajax, the others are exactly the same.
There must be a lot of requests for ajax in the project. We cannot modify $. ajax one by one when replacing jQuery. What should we do? In this case, we can add an adapter:
var $ = { ajax:function (options){ return ajax(options); }}
In this way, the old Code and the new interface can be compatible, avoiding modifications to the existing code.
Summary
The principle of the adapter mode is very simple, that is, to add a packaging class to encapsulate the new interface to adapt to the call of the old Code, avoid modifying the interface and calling code.
Applicable scenarios: There are many code calls to the old interface. To avoid modifying the old Code and replacing the new interface, the existing implementation method is not affected.
1. Applicable scenarios of the adapter mode:
The adapter is applicable to scenarios where the interface the customer system expects is not compatible with the interface provided by the existing API. The two methods adapted by the adapter should execute similar tasks. Otherwise, the problem cannot be solved. Just like bridging and appearance elements, you can create an adapter to isolate abstraction from its implementations so that the two can change independently.
2. Benefits of the adapter mode:
Use a new interface to package existing class interfaces, so that the customer program can use this class that is not tailored for it without the need for a large operation.
3. disadvantages of setting the hosts mode:
Some people think that the adapter is unnecessary overhead and can be completely avoided by rewriting existing code. In addition, the adapter mode also introduces a number of new tools to be supported. If the existing API is not finalized, or the new interface is not, the adapter may not always work.
When it involves large systems and legacy frameworks, its advantages tend to be more prominent than its disadvantages.