Click here to Skip to main content
Click here to Skip to main content

Adapter Design Pattern

, 4 May 2011
Rate this:
Please Sign up or sign in to vote.
Adapter Design Pattern clearly explained with example

You can see this and other great articles on design patterns here.

The Adapter Design Pattern allows you to make an existing class work with other existing class libraries without changing the code of the existing class.

We often need to use the methods of an existing class to work with other existing libraries. The way to do this is by creating another class, named the Adapter, that inherits from the existing class while implementing the interface of the existing library. The end result is that the Adapter can call the method of the existing class (since the Adapter inherits from the existing class) and can work in the existing library (since the Adapter implements the interface of the existing library).

Below is the UML of the Adapter Design Pattern:

  • The Adaptee is the existing class.
  • The IInterface is the interface defined in the existing library.
  • The Adapter is the class that you create, it is inherited from the Adaptee class and it implements the IInterface interface. Notice that it can call the OperationA method(inherited from the Adaptee) inside its OperationB method (implemented by the IInterface).

Let's do an example. Using the example given in the composite design pattern, where we had an organization tree that was constructed where all the employees implement the IEmployee interface. The IEmployee interface is from the existing library.

You are then given the Consultant class and you need to plug this Consultant class into the organization tree. The Consultant class is the Adaptee.

The way to do this is by creating the adapter class named the EmployeeAdapter, that inherits from the Consultant class while it implements the IEmployee interface:

In the Adapter, we can then call the methods from the parent class to mimic the behaviors needed in the common interface. In the EmployeeAdapter class, we can call the ShowSmile method from the parent Consultant class in its implementation of the IEmployee interface, which requires the ShowHappiness method. 

 

Below are the implementation code and the output of the Adapter Design Pattern from the example. Notice that we don't need to change the existing code in the Consultant class, yet we can plug its functionality into the existing IEmployee interface:

using System.Collections.Generic;    

class Program
{
    static void Main(string[] args)
    {
        List<iemployee> list = new List<iemployee>();
        list.Add(new Employee("Tom"));
        list.Add(new Employee("Jerry"));
        list.Add(new EmployeeAdapter("Bruno"));  //consultant from existing class
        ShowHappiness(list);
    }

    //*** Code below from the existing library does not need to be changed ***
    static void ShowHappiness(List<iemployee> list)
    {
        foreach (IEmployee i in list)
            i.ShowHappiness();
    }
}

//from the existing library, does not need to be changed
public interface IEmployee
{
    void ShowHappiness();
}

public class Employee : IEmployee
{
    private string name;
    
    public Employee(string name)
    {
        this.name = name;
    }

    void IEmployee.ShowHappiness()
    {
        Console.WriteLine("Employee " + this.name + " showed happiness");
    }
}

//existing class does not need to be changed
public class Consultant
{
    private string name;

    public Consultant(string name)
    {
        this.name = name;
    }

    protected void ShowSmile()
    {
        Console.WriteLine("Consultant " + this.name + " showed smile");
    }
}

public class EmployeeAdapter : Consultant, IEmployee
{
    public EmployeeAdapter(string name) : base(name)
    {
    }

    void IEmployee.ShowHappiness()
    {
        base.ShowSmile();  //call the parent Consultant class
    }
}

Liked this article? You can see this and other great articles on design patterns here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
SuggestionVery nice work, but this won't work with sealed classes PinmemberPRouleau3219-Nov-13 7:32 
GeneralMy Vote of 5 Pinmembervishakhakhadse17-Feb-13 16:41 
Excellent and easy to understand article. Thanks!
GeneralMy vote of 5 PinmvpKanasz Robert25-Jun-12 8:58 
Questiongreat PinmemberAshraf Hesham8-May-12 10:22 
QuestionNice and Concise PinmemberRajibdotnet053-Apr-12 14:36 
GeneralMy vote of 5 PinmemberRajibdotnet053-Apr-12 14:35 
GeneralMy vote of 5 PinmemberJacek Gajek1-May-11 11:13 
GeneralImages and links to devlake is broken Pinmembermeraydin1-May-11 5:55 
GeneralMy vote of 5 Pinmemberraja_krish29-Apr-11 6:05 
GeneralMy vote is 5 PinmemberShahriar Iqbal Chowdhury29-Apr-11 4:59 
GeneralMy vote of 5 PinmemberSantanu Tripathy28-Apr-11 21:03 
GeneralMy vote of 5 PinmemberOshtri Deka26-Apr-11 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid