The Adapter Pattern
Tags: csharp, designpatterns, general, programming
Published: 2021-05-04 13:01:20

---
title: The Adapter Pattern
published: true
date: 2021-05-04 08:00:21 UTC
tags: Csharp,DesignPatterns,General,Programming
canonical_url: https://garywoodfine.com/the-adapter-pattern/
---

The Adapter Pattern is the first [software design pattern](https://garywoodfine.com/software-design-patterns/ "Software Design Patterns | Gary Woodfine") of the Structural Pattern, that the Gang of Four (GOF) Design Patterns, presented in their book , [Design Patterns – Elements of Reusable Object-Oriented Software](https://amzn.to/2PdkTck).

The Adapter pattern is a **structural design pattern**  that enables incompatible interfaces, from disparate systems to exchange data and work together. It is extremely useful when integrating tool-kits, libraries and other utilities together.

Apple Macbook users will be very familiar adaptors, which they will frequently use to plug various devices and network connections.

[![](https://i2.wp.com/garywoodfine.com/wp-content/uploads/2021/04/adapter-pattern.jpg?resize=1000%2C852&ssl=1)](https://amzn.to/3gHAkXX)<figcaption>USB C Hub Multiport Adapter, 13-in-1 MacBook Pro USB Type C Adapter Hub with Ethernet, VGA, 4K HDMI, 3 USB 2.0, 2 USB 3.0, 3-Slot Card Reader, 3.5mm Audio and Power Delivery</figcaption>

This is essentially a physical implementation of the Adapter pattern. [Head First Design Patterns](https://amzn.to/3tSqSVo) provides a really good succinct definition of the Adapter Pattern.

> he Adapter pattern allows you to provide an object instance to a client that has a dependency on an interface that your instance does not implement. An Adapter class is created that fulfils the expected interface of the client but that implements the methods of the interface by delegating to different methods of another object.
> 
> <cite><a href="https://amzn.to/2JfPh15" data-type="URL" data-id="https://amzn.to/2JfPh15" target="_blank" rel="noreferrer noopener">Adaptive Code – Agile coding with design patterns and SOLID principles</a></cite>

[![](https://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=1509302581&Format=_SL250_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=threenine05-20&language=en_US)](https://www.amazon.com/Adaptive-Code-principles-Developer-Practices/dp/1509302581/ref=as_li_ss_il?crid=3LTHTHYU2QU4U&keywords=adaptive+code+via+c#&qid=1571847043&sprefix=adaptive+code,aps,239&sr=8-1&linkCode=li3&tag=threenine05-20&linkId=f8cf487baf5c73c6a1a6ef73ae5771de&language=en_US) ![](https://ir-na.amazon-adsystem.com/e/ir?t=threenine05-20&language=en_US&l=li3&o=1&a=1509302581)

[Adaptive Code](https://amzn.to/2JfPh15)  
[Agile coding with design patterns and SOLID principles](https://amzn.to/2JfPh15)

### Two types of Adapters


There are typically two kinds of adapter pattern implementations


- **_Object_** Adapters
- _ **Class** _ Adapters

#### _Class adapters_ 

Typically you may only really encounter this type of pattern when using C or C++ or other languages that enable multiple inheritance.

#### _Object adapters_ 

It basically the only adapter pattern C# developers have available and is the type that is discussed in this post.

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3"></path></svg>

The Adapter Pattern

Converts the interface of a class into another interface this client expects.   
  
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

The object Adapter pattern uses composition to delegate from the methods of the interface to those of a contained encapsulated class. This is a more common implementation of the Adapter pattern.

The main advantage of this pattern is that it enables the use of another library in an application that has an incompatible interface by using an adapter that does the conversion.

[![](https://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&ASIN=149207800X&Format=_SL250_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=threenine05-20&language=en_US)](https://www.amazon.com/Head-First-Design-Patterns-Object-Oriented/dp/149207800X?dchild=1&keywords=head+first+design+patterns&qid=1619728872&sr=8-1&linkCode=li3&tag=threenine05-20&linkId=19586b912cfcb47ce30fadec2004ac0b&language=en_US&ref_=as_li_ss_il) ![](https://ir-na.amazon-adsystem.com/e/ir?t=threenine05-20&language=en_US&l=li3&o=1&a=149207800X)

[Head First Design Patterns](https://amzn.to/32YhjIT)  
[Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/32YhjIT)

### _When to use the Adapter Pattern?_

There are a number of situations when making use of the Adapter pattern can be a great solution

- A class needs to be reused that does not have an interface that a client requires.
- Allow a system to use classes of another system that is incompatible with it.
- Allow communication between a new and already existing system that is independent of each other.
- Sometimes a toolkit or class library cannot be used because its interface is incompatible with the interface required by an application.

### _Simple Adapter pattern implementation_

{% github garywoodfine/software-design-patterns %}

In its most simple form the Adapter Pattern can just be a simple _ **wrapper** _ class which implements an interface. However the implementation within the class my implement a different set of classes to deliver the functionality required.

```csharp
You may have an interface for Transport class that defines a Commute method.

public interface ITransport
{
   void Commute();
}
```csharp

However, the only class you have a available is `Bicycle` class that has a `Pedal` method which will work for what you need.

```csharp
public class Bicycle
{
   public void Pedal()
   {
       Console.WriteLine("Pedaling");
   }
}
```

The snag is that the method or class that is going to use it, can only use classes that implement the `ITransport` interface.

We can use the Adapter pattern here to create a class that implements the `ITransport` interface, but it actually just wraps the `Bicycle` class.

```csharp
public class Transport : ITransport
{
   private Bicycle _bike => new Bicycle();
   public void Commute()
   {
     _bike.Pedal();
   }
 }
```

You can now implement the `Transport` class in your application, because it implements the `ITransport` interface.

```csharp
 class Program
 {
    static void Main(string[] args)
    {
       var transport = new Transport();
       transport.Commute();
    }
 }
```

That is all there is to the Adapter pattern. Even in this most simplistic of implementations you can see the power of enabling classes that my seem incompatible with your application , yet you can still make use of them.

The adapter pattern only needs to do as much as it needs to do in order to adapt a class to work. If the _Target_ and _Adaptee_ are similar then the adapter has just to delegate the requests from the Target to the Adaptee. If _Target_ and _Adaptee_ are not similar, then the adapter might have to convert the data structures between those and to implement the operations required by the Target but not implemented by the Adaptee.

### _Conclusion_ 

The Adapter Pattern is a very simple pattern, but can be quite powerful and extremely useful, and is really a worthwhile pattern to be aware of. Many developers, will most likely have used the Adapter pattern, without actually being explicitly aware of it.

The are more advanced implementation details of the Adapter pattern, but the fundamentals of the pattern remain the same.