Home > Web Front-end > JS Tutorial > js design patterns: what is the adapter pattern? Introduction to js adapter pattern

js design patterns: what is the adapter pattern? Introduction to js adapter pattern

不言
Release: 2018-08-17 17:00:10
Original
2511 people have browsed it

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.

What is the adapter pattern?

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.

js adapter pattern example

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));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template