Structural Design Pattern: Adapter

As a continuance with the Software Design Pattern that  begun with the Singleton Pattern last week, today I will discuss and demonstrate the Adapter Design Pattern.

Adapter Pattern

The Adapter Design Pattern does exactly as one would presume, it allows for incompatible interfaces to communicate through an adaption layer. The finest example of an adapter is using the electrical energy system, specifically plugs.

European Plug Socket  UK Plug

Image Courtesy of

As you can imagine, the above plug (UK Standard) will not fit the socket (European Standard Wall Socket). However, this does not mean a device using the BS 1363-1 standard plug (UK Standard, Above) won’t be able to use a European plug socket. It is in this situation that we would use an Adapter to ensure the two incompatible interfaces can interact with each other, without ever knowing they have been manipulated.

UK - EU Plug Adapter

By plugging the devices B 1363-1 standard plug into once side of the device, and plugging the adapter device into the European Standard wall socket, electricity can be received by the device without needing to know an adapter has been used.

The Adapter Pattern has also been referred to as a Wrapper or a Translator pattern

The Ingredients

The Adapter pattern is made up from four separate components, allowing us to wrap, translate or adapt the required functionality in a fashion suitable for the client to consume.

The Target is the interface used by the client in order to achieve some functionality. The Adaptee holds the functionality that the client wants, the Client itself wants to achieve the functionality of the Adaptee, and the Adapter makes this possible, by acting as a convertor between the Adaptee and the Target.

In the form of a class diagram, this looks like;

Adapter Pattern

Within C#, We can implement the Adapter pattern as follows;

The following is the definition for an Target;

interface IProductNameProvider


    string GetProductName();


Which is used by the Client;

public void SomeClientMethod()


    IProductNameProvider nameProvider = new ProductNameProvider();

    string productName = nameProvider.GetProductName();


Which makes ProductNameProvider our Adapter;

public class ProductNameProvider : IProductNameProvider
    private ProductProvider _productProvider = new ProductProvider();

    public string GetProductName()
        Product product = _productProvider.GetProduct();
        return product.Name;


And finally, ProductProvider is the Adaptee;

public class ProductProvider
    public Product GetProduct()
        //Logic to return a Product Object

In this Adapter Example, the client requires to know the name of a product, without requiring any other information. The ProductProvider class, the Adaptee, has the means to do so, but returns the entire product object, which isn’t required by the client.

In steps the Adapter, in a Class Adapter role (See Below.) which takes the returned data from the Adaptee and converts it into a format that the client can process. The client accesses the Adapter through the Target interface.

Class Adapter vs Object Adapter

When using the Adapter Pattern it is good to be aware that there are two different types of Adapter, Class and Object.

A Class Adapter uses composition, and creates an instance of the Adaptee.

An Object Adapter uses Inheritance, and derives from the Adaptee.

Whilst both cases are valid when using the Adapter Pattern, it can never hurt to know the difference.

The Usage

Adapter Patterns are commonly found when working in systems that maintain legacy code. This is typically due to a legacy method maintaining the logic required for a newly created client, though transferring the data in a format that the client cannot accept. Perhaps there it more data than needed being returned, and only a single piece of information is required (Such as the example above).

No matter the usage, the Adapter Pattern is ideal for making the triangle fit through the square shaped hole.

About Adam Riddick

When not Developing, Adam moonlights as a climbing frame for his two children. Adam Riddick was founded in 1987, in County Durham, England. Read More ...

08. December 2014 by Adam Riddick
Categories: Achitecture, Design & Practice | Leave a comment

Inspire me with your wisdom ...