You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Software gets XML response from an external API.
To show data analysis (charts, graphs) the application likes to use a third-party library.
The third-party library only accepts JSON as input. However, the software has XML data.
Due to this incompatibility between XML and JSON, our software can't use the third-party library directly.
Solution
The Pattern solves the above problem by defining a class (Adapter class) that converts the XML data to JSON and uses the third-party library for data analysis.
Class Structure
Different Classes/Interfaces
Client Class: The Client class contains the business logic.
Client Interface: It describes a protocol that other classes must follow to be able to collaborate with the client class
Service Class: The Service is some useful class (usually 3rd-party or legacy). The client can’t use this class directly because it has an incompatible interface.
Adapter Class: The Adapter class acts as a mediator between the client and service object. It implements the client interface and wraps the service object, translating client calls into a format that the service object can understand.
UML Diagram
Example Code
RoundHole.java has a class with a method fits that has a parameter of type RoundHole.
The fits method is only compatible with the RoundHole class object.
SquarePeg.java has a class. We want to use this class object with the fits method of the RoundHole class.
The above point is not possible because the fits method only accepts RoundPeg type objects.
To make it work, we have an adapter class SquarePegAdapter. It extends RoundPeg class and uses SquarePeg class.
The object of the SquarePegAdapter class will have a type of RoundPeg and hence it is compatible with the fit method of RoundHole class.
SquarePegAdapter class object uses SquarePeg class object.
This way, RoundHole class can communicate with code of SquarePeg class.