Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Adapter Adapter pattern

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to apply the Adapter adapter pattern". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to apply the Adapter adapter pattern.

Adapter (Adapter Mode)

Adapter (Adapter pattern) is a structural pattern, alias wrapper. Structural patterns focus on how to combine classes and objects to get a larger structure. We spend most of our time dealing with this design pattern.

Intention: convert the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.

The intention of this design pattern is to smooth out the interface incompatibility. Note that it can only solve the problem of interface inconsistency, but not functional inconsistency.

As an example

If you do not understand the above intention introduction, it does not matter, the design pattern needs to be used in daily work, combined with examples can deepen your understanding, below I have prepared three examples to let you experience in what scenarios this design pattern will be used.

Interface converter

There are many kinds of sockets, and we have used many adapters to convert different plugs so that they can be used normally without replacing the sockets.

The conversion of USB interfaces is also wonderful, including converting TypeC interfaces to TypeA and TypeA interfaces to TypeC, supporting two-way conversion.

The interface converter is the adapter pattern we use in our daily life, because the manufacturer does not produce a new socket, and we do not change a mobile phone because the interface does not fit. All we need is an interface converter. This is the benefit of using the design pattern.

Database ORM

ORM shields the SQL layer, which brings the advantage that there is no need to understand the differences between different SQL grammars. For general functions, ORM will transform SQL according to different platforms, such as Postgresql and Mysql.

For ORM, shielding the differences between different platforms is done using the adapter pattern.

API Deprecated

When a widely used library is upgraded with break change, it often gives developers enough time to upgrade, and cannot be directly hung up after the upgrade, so the discarded API should be marked as deprecated, and the actual implementation of this kind of abandoned marked API is often replaced by a new API. This scenario uses the adapter pattern to adapt the new API to the old API to implement API Deprecated.

Intention interpretation

The above three examples meet the following two conditions:

API incompatibility: because of different interfaces; different database SQL syntax; different framework API.

But the capabilities are supported: sockets have the ability to charge or read; different SQL have the ability to query the database; the new API covers the ability of the old API.

In this way, the intent of Adapter can be met through the adapter:

Intention: convert the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.

Structure diagram

The implementation of the adapter is divided into inheritance mode and combination mode.

The following is the noun explanation:

Adapter adapter, adapting Adeptee to Target.

Content that Adaptee is adapted to, such as incompatible interfaces.

The content that the Target adapts to, such as the interface you need to use.

Inheritance:

The adapter inherits Adaptee and implements Target, which is applicable in scenarios where the structure of Adaptee is similar to that of Target, because only partial differentiation is required.

Combination:

The combination is more expansive, but the workload is larger. If the structure of Target is different from that of Adaptee, it is suitable to use the combination mode.

Code example

The following example is written in typescript.

Inheritance:

Interface ITarget {

/ / the standard mode is hello

Hello: () = > void

}

Class Adaptee {

/ / the class method to be adapted is called sayHello

SayHello () {

Console.log ('hello')

}

}

/ / the adapter inherits Adaptee and implements ITarget

Class Adapter extends Adaptee implements ITarget {

Hello () {

/ / use sayHello to connect to hello

Super.sayHello ()

}

}

Combination:

Interface ITarget {

/ / the standard mode is hello

Hello: () = > void

}

Class Adaptee {

/ / the class method to be adapted is called sayHello

SayHello () {

Console.log ('hello')

}

}

/ / the adapter inherits Adaptee and implements ITarget

Class Adapter implements ITarget {

Private adaptee: Adaptee

Constructor (adaptee: Adaptee) {

This.adaptee = adaptee

}

Hello () {

/ / use adaptee.sayHello to connect to hello

This.adaptee.sayHello ()

}

}

Malpractice

Using the adapter pattern can be a problem in itself, because there should be no overseas Chinese within a good system, and the model should be consistent. Consider using the adapter pattern only if:

The new and old systems are replaced, and the cost of transformation is very high.

The tripartite package fits.

New and old API are compatible.

Unify the interfaces of multiple classes. It can generally be used in combination with the factory method.

Thank you for reading, the above is the content of "how to apply the Adapter adapter pattern". After the study of this article, I believe you have a deeper understanding of how to apply the Adapter adapter pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report