BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Adapter Design Pattern

The adapter pattern is a design pattern that is used to allow two incompatible types to communicate. Where one class relies upon a specific interface that is not implemented by another class, the adapter acts as a translator between the two types.

What is the Adapter Pattern?

The adapter pattern is a Gang of Four design pattern. This is a structural pattern as it defines a manner for creating relationships between classes. The adapter design pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.

Let's take an example where we have a personnel system and an intranet solution that displays, amongst other things, a telephone list. It may be that the personnel system includes methods that permit the retrieval of employees, including their name, job title and telephone number. The intranet solution may include a plug-in system that can be used to source this data but the plug-in must provide a specific interface that is not supported by the personnel software.

In this case, we can create a new class to be an adapter. This class will provide the intranet's desired interface and will hold an object of the type required by the personnel system. When the intranet makes a request, this request can be passed to the correct method of the personnel system. The response from the personnel system will then be translated to a format that can be used by the intranet.

Implementing the Adapter Pattern

Adapter Design Pattern UML

The UML class diagram above describes an implementation of the adapter design pattern. The items in the diagram are described below:

  • Client. The client class is that which requires the use of an incompatible type. It expects to interact with a type that implements the ITarget interface. However, the class that we wish it to use is the incompatible Adaptee.
  • ITarget. This is the expected interface for the client class. Although shown in the diagram as an interface, it may be a class that the adapter inherits. If a class is used, the adapter must override its members.
  • Adaptee. This class contains the functionality that is required by the client. However, its interface is not compatible with that which is expected.
  • Adapter. This class provides the link between the incompatible Client and Adaptee classes. The adapter implements the ITarget interface and contains a private instance of the Adaptee class. When the client executes MethodA on the ITarget interface, MethodA in the adapter translates this request to a call to MethodB on the internal Adaptee instance.

The following shows the basic code of the adapter design pattern implemented using C#. In this case, the object that implements ITarget is passed to the client's constructor. However, it could quite easily be provided as a parameter of a method.

public class Client
{
    private ITarget _target;

    public Client(ITarget target)
    {
        _target = target;
    }

    public void MakeRequest()
    {
        _target.MethodA();
    }
}


public interface ITarget
{
    void MethodA();
}


public class Adaptee
{
    public void MethodB()
    {
        Console.WriteLine("MethodB called");
    }
}


public class Adapter : ITarget
{
    Adaptee _adaptee = new Adaptee();

    public void MethodA()
    {
        _adaptee.MethodB();
    }
}
2 December 2008