Adapter Design Pattern Explained Simply

This article will explain the adapter design pattern , its practical use case with its benefits and drawbacks.

ADAPTER DESIGN PATTERN

 
Adapter pattern is placed under the category of structural design pattern and this pattern is used to allow communication between two incompatible interfaces by acting as a bridge.

This pattern involves a single class called adapter which is responsible for communication between two independent or incompatible interfaces.

Adapter pattern is also called Translator pattern or Wrapper pattern.

Some of the real-life examples are,

  1. A card reader acts as an adapter between a memory card and a laptop
  2. Universal Charger also acts as an adapter between the power source and the charger.
Now, let’s see the UML diagram of the same as shown below,
 
Adapter Design Pattern
image Source: dotnet tricks.

ITarget

This is an interface which has been used by the client to make a request.

Adapter

This is a class which implements the ITarget interface and inherits the Adaptee class. It is responsible for communication between Client and Adaptee. In our example, ResponsesStore is an Adaptee class.

Adaptee

This is a class which has the functionality required by the client. However, its interface is not compatible with the client.

Client

This is a class which interacts with a type that implements the ITarget interface.
 

PRACTICAL USE CASE

 
Imagine an online portal which displays the responses received for surveys from the survey recipients on its home page.

These responses are coming from a third-party vendor with which the portal has tied hands to get responses from.

The third-party vendor already has an inventory system in place which can give the list of responses for a particular survey as this third-party system is actually responsible for sending the survey to the recipients.

And there is no interface available to the online portal (Client) with which the portal can call the third-party vendor’s code to get the actual responses for a survey(s) which are being displayed on the portal home page.

Since we want to call the third-party vendor’s code which is incompatible with client (online portal) code we can apply “Adapter design pattern” here.

 ITarget

A method which the online portal calls to get the list of responses and this request has been encapsulated in this interface.

Adaptee

The third-party vendor’s code which gives us the list of responses. Here ResponsesStore is the Adaptee class.

Adapter

The wrapper which would implement ITarget and would call third-party vendor’s code.

Client

The online portal code which gets the list of responses and then displays them.

 

  1. void Main() {  
  2.     var client = new Client(new Adaptar());  
  3.     var userResponses = client.GetResponsesRecieved();  
  4.     userResponses.ForEach(p => p.Dump());  
  5. }  
  6. publicinterface ITarget {  
  7.     List < string > GetResponses();  
  8. }  
  9. publicclass Adaptar: ITarget {  
  10.     public List < string > GetResponses() {  
  11.         returnnew ResponsesStore().GetResponsesRecieved();  
  12.     }  
  13. }  
  14. publicclass ResponsesStore {  
  15.     public List < string > GetResponsesRecieved() {  
  16.         var responses = new List < string > () {  
  17.             "This is a test response by user 1",  
  18.             "This is a test response by user 2",  
  19.             "This is a test response by user 3",  
  20.             "This is a test response by user 4"  
  21.         };  
  22.         return responses;  
  23.     }  
  24. }  
  25. publicclass Client {  
  26.     private ITarget _target;  
  27.     public Client(ITarget target) {  
  28.         _target = target;  
  29.     }  
  30.     public List < string > GetResponsesRecieved() {  
  31.         return _target.GetResponses();  
  32.     }  
  33. }  

 

Output

Adapter Design Pattern

WHEN TO USE IT

This is really important to understand as the existence of design pattern doesn’t mean that we should use it just for the sake of using it; its actual use case is also important to understand. Thus, this pattern will be useful in the below-mentioned scenarios:

  1. Allow communication between the new and already existing system which are independent of each other.
  2. The Adapter design pattern is easy to implement and is quite common when legacy code is involved.

BENEFITS AND DRAWBACKS

 
It makes your code more structured and also supports the open/closed principle as you can add new adapters without disturbing the existing client code logic.

The complexity increases as you need to include a new interface or classes.

I hope you find this article helpful. Stay tuned for more … Cheers to coding!!