The nature and classification of the adapter mode

What is an adapter?
The nature and classification of the adapter mode

In computer programming, the adapter pattern (sometimes called the wrapper style or wrapper) transforms the interface of one class into another interface that the client expects, so that the two classes that could not work together due to interface mismatches Can work together. An adaptation allows classes that normally cannot work together because the interfaces are incompatible, by wrapping the class's own interface in an existing class.

The roles involved in the adapter mode are:

● Target role : This is the interface that you expect. Note: Since the class adapter mode is discussed here, the target cannot be a class.

● Source (Adapee) role : The interface that needs to be adapted now.

● Adapter (Adaper) role : Adapter class is the core of this mode. The adapter converts the source interface to the target interface. Obviously, this role cannot be an interface, but must be a concrete class.

The nature of the adapter pattern
The nature and classification of the adapter mode

The essence of the adapter mode is: conversion matching, multiplexing function .

The adapter calls the existing implementation through the conversion, so that the existing implementation can be matched to the required interface to meet the needs of the client. That is to say, conversion matching is a means, and multiplexing existing functions is the purpose.

In the process of conversion matching, the adapter can also implement some functional processing before and after the conversion call, that is, implement intelligent adaptation.

Adapter classification

There are two types of adapter modes:

Class Adapter Mode -- In this adapter mode, the adapter inherits from the implemented class (general multiple inheritance).

Class adapter mode

1. Match Adaptee and Taget with a concrete Adapter class. The result is that when we want to match a class and all its subclasses, the class Adapter will not be able to work.

2. Enables the Adapter to override (redefined) the partial behavior of the Adaptee because the Adapter is a subclass of Adaptee.

Object Adapter Mode -- In this adapter mode, the adapter holds an instance of the class it wraps. In this case, the adapter calls the physical entity of the wrapped object.

The nature and classification of the adapter mode

1. Allow an Adapter to work with multiple Adaptees, that is, Adaptee itself and all its subclasses (if there are subclasses). The Adapter can also add functionality to all Adaptees at once.

2. Make it more difficult to override (redefined) Adaptee. If you must override the Adaptee method, you have to do a Adaptee subclass to override the Adaptee method, and then adapt the subclass as a real Adaptee source.

Interface Adapter Pattern -- When you don't want to implement all the methods in an interface, you can create an abstract class Wrapper to implement all methods. When we write other classes, we can inherit the abstract class.

When do you use an adapter?
Class adapter mode

Adapter mode is not a frequently used design mode. In the following cases, an adapter is required.

1. The system needs to use existing classes, and such interfaces do not meet the needs of the system.

2. You want to create a reusable class for working with classes that don't have much to do with each other, including some that might be introduced in the future. These source classes don't necessarily have a consistent interface.

3. Insert a class into another class through interface conversion. (For example, tigers and birds, there is now a flying tiger. Adding an adapter without increasing the physical requirements, and having a tiger object in it, realizes the flying interface.)

Excessive use of the adapter will make the system very messy, and it is not easy to grasp the whole. For example, it is obvious that the A interface is called. In fact, the internal interface is adapted to be implemented as a B interface. If there is too much such a system, no Different from a disaster. So if it's not necessary, you can refactor the system without using an adapter.

Onlyrelx LUX3000

Onlyrelx LUX3000 disposable vape pen is portable and fashion disposable electronic cigarette, disposable ecigs pen are trending featured vape pen for vapors as it's safety and easy to use. Disposable vape pod,disposable vape, wholesale vape,vape wholesale,vape pen manufacturer and supplier.disposable vape pen,disposable electronic cigarette,disposable ecigs pen,disposable ecigs stick,disposable e-cigs pen,disposable vape factory,disposable vape pod,disposable vape device,vape pen,vape stick, vape wholesale,wholesale vape,customized dispsoable vape pen,customized vape pen,OEM&ODM disposable ecigs pen,disposable electronic cigarette wholesale, wholesale disposable electronic cigarette,distribute vape pen,vape pen distribute,high quality vape pen,high quality vape pod.

Onlyrelx Lux3000,Disposable Vape Juice,Mini Disposable Vaporizer Pen,Disposable Vaporizer Pen

Shenzhen Onlyrelx Technology Co.,Ltd , https://www.onlyrelxtech.com