Home >Web Front-end >JS Tutorial >js design patterns: what is the adapter pattern? Introduction to js adapter pattern
This article brings you content about js design patterns: What is the adapter pattern? The introduction of js adapter mode has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Definition: Convert the interface of a class into another interface that the customer wants. The Adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.
Main solution: Main solution: In software systems, it is often necessary to put some "existing objects" into a new environment, and the interfaces required by the new environment cannot be satisfied by the existing objects. .
When to use: 1. The system needs to use existing classes, but this type of interface does not meet the needs of the system. 2. You want to create a class that can be reused to work with some classes that are not much related to each other, including some classes that may be introduced in the future. These source classes do not necessarily have consistent interfaces. 3. Insert one class into another class through interface conversion. (For example, tigers and birds now have a flying tiger. Without the need to add entities, add an adapter to contain a tiger object and implement the flying interface.)
How to solve: Inherit or depend on (recommended).
Key code: The adapter inherits or relies on existing objects to achieve the desired target interface.
js adapter mode application example: 1. American electrical appliances are 110V and China is 220V. An adapter is required to convert 110V into 220V. 2. JAVA JDK 1.1 provides the Enumeration interface, and 1.2 provides the Iterator interface. If you want to use the 1.2 JDK, you must convert the Enumeration interface of the previous system into the Iterator interface. In this case, the adapter mode is required. 3. Run the WINDOWS program on LINUX. 4. jdbc in JAVA.
Advantages of js adapter pattern: 1. Any two unrelated classes can be run together. 2. Improved class reuse. 3. Increased the transparency of the class. 4. Good flexibility.
Disadvantages of js adapter mode: Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that the A interface is called, but in fact it is internally adapted to the implementation of the B interface. If this happens too much in a system, it will be tantamount to a disaster. Therefore, if it is not necessary, you can reconstruct the system directly without using the adapter.
js adapter pattern application scenario: Motivation to modify the interface of a normally running system, then you should consider using the adapter pattern.
Note: The adapter is not added during detailed design, but to solve problems of the project in service.
For example, we need to adapt the old interface to a new interface
// 老接口 const oldCity = (function() { return [ { name: 'hangzhou', id: 11, }, { name: 'jinhua', id: 12 } ] }()) // 新接口希望是下面形式 // { // hangzhou: 11, // jinhua: 12 // } // 这时候就可采用适配者模式 const adaptor = function(oldCity) { const obj = {}; for (let city of oldCity) { obj[city.name] = city.id } return obj }; console.log(adaptor(oldCity));
Related recommendations:
js design pattern : What is the mediator pattern? Introduction to the js mediator pattern
js design pattern: What is the state pattern? Introduction to js state mode
The above is the detailed content of js design patterns: what is the adapter pattern? Introduction to js adapter pattern. For more information, please follow other related articles on the PHP Chinese website!