Adapter mode (Adapter)



 

GOF Intent: Convert the interface of a class to another interface that the client wants.

Adapt mode enables classes to work together that would otherwise not work together due to incompatible interfaces . 

 

Role:

1 Target interface (Target): The interface expected by the client. Targets can be concrete or abstract classes, or interfaces.

2 Adapter (Adapter): Convert the original interface to the target interface by wrapping an object that needs to be adapted.

3 The class to be adapted (Adaptee): the class that needs to be adapted or the class to be adapted.

 

Implementation: slightly

 

Applicable scene

1 The system needs to use existing classes whose interfaces do not conform to the system's interface.

2 Want to create a reusable class to work with some classes that are not much related to each other, including some classes that may be introduced in the future.

3 When two classes do the same or similar things, but have different interfaces.

4 The class developed by the old system has already implemented some functions, but the client can only access it in the form of another interface, but we do not want to manually change the original class.

 

5. When using third-party components, the component interface definition is different from that defined by yourself. You do not want to modify your own interface, but you need to use the functions of the third-party component interface.

 

advantage:

1 Through the adapter, the client can call the same interface, so it is transparent to the client. Doing so is simpler, more direct, and more compact.

2 The existing classes are reused, which solves the problem of inconsistent requirements between the existing classes and the reuse environment.

3 Decouple the target class and the adapter class, and reuse the existing adapter class by introducing an adapter class without modifying the original code.

 

4 An object adapter can adapt multiple different adapter classes to the same target, that is, the same adapter can adapt both the adapter class and its subclasses to the target interface.

 

shortcoming:

For object adapters, the implementation process of replacing adapters is more complicated

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326795442&siteId=291194637