Sometimes during the development process, we will find that the interfaces required by the client and the provided interfaces are incompatible. We cannot modify the client interface for special reasons. In this case, we need to adapt to existing interfaces and incompatible classes, which requires the adapter mode. With adapters, we can use them without modifying old code, which is the ability of the adapter.
Adaptation mode can be used to adapt between an existing interface and an incompatible class. Objects using this mode are also called wrappers because they are wrapping another object with a new interface.
On the surface, the adapter mode is much like the appearance mode. They all need to wrap other objects and change the interfaces they render. The difference between the two is how they change the interface. The appearance element presents a simplified interface that does not provide additional options, and sometimes it makes some assumptions to facilitate common tasks. The adapter converts one interface to another, which will not filter out certain capabilities and will not simplify the interface. If the client system API is not available, an adapter is required.
Basic Theory
Adapter mode: convert an interface into an interface required by the client without modifying the client code, so that incompatible code can work together.
The adapter mainly consists of 3 roles:
(1) Client: the class that calls the interface
(2) Adapter: Class used to connect client interface and interface providing services
(3) Adapter: Provides services, but is incompatible with client interface requirements.
Implementation of adapter mode
1. The easiest adapter
The adapter mode is not as complicated as you think, so let's give you the simplest example.
The client calls a method for addition calculation:
var result = add(1,2);
However, we did not provide the add method, and provide the sum method with the same similar function:
function sum(v1,v2){ return v1 + v2;}In order to avoid modifying the client and server, we add a wrapper function:
function add (v1,v2){ reutrn sum(v1,v2);}This is the simplest adapter mode, we add a wrapper method between two incompatible interfaces, and use this method to connect the two to make them work together.
2. Practical application
With the development of front-end frameworks, more and more developers have begun to use the MVVM framework for development, only needing to manipulate data without DOM elements, and jQuery has less and less effect. Many projects still refer to the jQuery library tool class, because we need to use the ajax provided by jQuery to request data to the server. If jQuery's role in the project is only used as ajax tool library, it feels like it's a bit like killing a chicken and causing waste of resources. At this time, we can completely encapsulate our own ajax library.
Suppose that the ajax we encapsulated is used through a function:
ajax({ url:'/getData', type:'Post', dataType:'json', data:{ id:"123" }}).done(function(){})Except for the difference between calling interface ajax and jQuery's $.ajax, the others are exactly the same.
There must be many places in the project that request ajax. When we replace jQuery, we cannot modify $.ajax one by one. What should we do? At this time, we can add an adapter:
var $ = { ajax:function (options){ return ajax(options); }}This will be compatible with old code and new interfaces, avoiding modifications to existing code.
Summarize
The principle of the adapter mode is very simple, which is to add a new wrapper class to wrap the new interface to adapt to the calls of old code, and avoid modifying the interface and calling the code.
Applicable scenarios: There are many code calls old interfaces. In order to avoid modifying old code and replacing new interfaces, the application scenarios do not affect the existing implementation methods.
1. Applicable occasions for adapter mode:
Adapters are suitable for situations where the interfaces expected by the customer system are incompatible with those provided by the existing API. The two methods adapted to the adapter should perform similar tasks, otherwise the problem will not be solved. Just like bridge elements and appearance elements, by creating adapters, abstraction can be isolated from their implementations so that the two can be changed independently.
2. Adapter mode benefits:
Wrap the interface of an existing class with a new interface so that the client program can use this class that is not tailored for without major surgery.
3. Disadvantages of the orchestrator mode:
Some people think that adapters are an unnecessary overhead that can be avoided entirely by rewriting existing code. In addition, adapter mode will also introduce a batch of new tools that need to be supported. If the existing API is not yet shaped, or the new interface is not yet shaped, the adapter may not always work.
Its advantages tend to stand out more than its disadvantages when it involves large systems and legacy frameworks.