1 Adapter (Structural) Design Pattern

The Adapter Design Pattern is a structural design pattern that allows two incompatible interfaces to work together. It acts as a bridge between a client and an incompatible class, converting the interface of one class into an interface the client expects.

  • Adapter Pattern is applied when we have the problem.

Problem

You’re developing a weather monitoring application that retrieves weather data from multiple providers, processes it, and displays a user-friendly interface showing temperature, humidity, and wind speed.

Current State:

  • The weather data providers supply information in CSV format.
  • Your application processes this data and displays weather insights on a dashboard.

New Requirement:

You want to integrate a powerful third-party weather prediction AI library that provides advanced forecasts. However, the library only works with data in JSON format.

Challenge:

  • You cannot modify the existing CSV weather data providers.
  • You could think of changing the weather prediction AI library to work with CSV. However this might break some existing code that relies on the library. And worse, you might not have access to the library's source code making this approach impossible. 
  • You need to adapt the CSV data to JSON format so the prediction library can consume it.

Solution:

Use the Adapter Design Pattern:

  • Create an Adapter that converts the CSV weather data into JSON format.
  • The application continues to work with the CSV providers without modification.
  • The third-party prediction library receives the JSON data it requires.

Real-World Analogy for the Adapter Design Pattern

Scenario: Power Plug Adapter

Imagine you are traveling from the United States to Europe. The electrical outlets in Europe have a different shape and voltage compared to the outlets in the US. Your laptop charger has a US-style plug that cannot directly fit into European sockets.


Problem

  1. Incompatibility: Your laptop charger’s plug (US-style) is not compatible with European sockets.
  2. Need: You want to use your laptop in Europe without changing your charger or modifying the sockets.

Solution

You use a power plug adapter, a small device that connects your US-style plug to the European socket. The adapter "translates" the interface of your plug into the interface expected by the socket, allowing them to work together.

Components of the Adapter Design Pattern in This Example

  1. Target (European Socket):
    • The interface that your charger needs to be compatible with.
  2. Adaptee (US Charger):
    • The existing object with an incompatible interface.
  3. Adapter (Plug Adapter):
    • The object that connects the charger to the socket, converting the US plug format to the European socket format.
  4. Client (You):
    • The user trying to charge their device.

Key Characteristics

  • The adapter enables compatibility without modifying the charger or altering the socket.
  • It acts as a bridge between two incompatible interfaces.