Adapter Design Pattern

Henry Delgado, BCS, MBA
3 min readMay 6, 2017

An adapter design pattern helps in situations where we need to convert the interface of an existing class into a new interface that the client expects. Let us take a look at an example to help us understand this definition.

Example Problem To Solve

Imagine that Company X sends SMS notifications to its users after completing any online transaction. The company offers services in the United States and it is expanding its services to Honduras.

In the United States, Company X has been using a third party vendor called American SMS Services to send notifications. However, Company X will use a foreign third party vendor called Tigo SMS Services in Honduras. Both vendors have different business logic on how to send and receive SMS messages. Let’s take a look at the current architecture.

Current Architecture

Company X has its own custom Interface that defines the SMS services to provide. The following snippet shows the current interface with calls to get and send messages:

Company X connects to an API that American SMS Services provides to send and receive SMS notifications. American SMS Services has the following Interface:

The new third party vendor, Tigo SMS Services, has the following interface:

Users consuming services from Company X should not care about who is sending the message or how the message is been sent. However, Company X needs to identify the user to determine if the notification message will be sent by American SMS or Tigo SMS.

Company X needs to extend its functionality and implement a new interface without breaking or interrupting the current code. An adapter pattern is perfect to solve this design problem converting the interface of the existing class into the expected interface.

Solution

Create an implementation class (the adaptee) for each of the SMS vendors. Each implementation will map the Company X interface IMessageService from and to the vendor’s interface (IAmericanSMS or IServiciosTigo).

American SMS Implementation — Adaptee

Tigo SMS Implementation — Adaptee

Company X has an API that implements IMessageService and uses the adapter pattern to inject the proper third party vendor interface depending on the client requesting services. The following snippets is simulating that we connect to a database to gather information about what implementation should the client use. I said “simulate” because the following is just a static class for demonstration purposes.

The Adapter

We will need dependency injection to implement the interface client expect. At runtime, our adapter object will be initialized with a reference service available. Clients will call the adapter object, and the adapter object will forward these calls to the service available.

Summary

The adapter pattern allows us to use an existing interface and convert it to any implementation of the same interface. Company X is able to work with the same API code that implements IMessageService. The adapter pattern allows the company to work with any amount of implementations as needed for the same interface without disrupting the current API services.

--

--

Henry Delgado, BCS, MBA

A passionate software developer eager to learn, evaluate and share!