Click here to Skip to main content
14,579,712 members

Adapter Design Pattern

Rate this:
4.80 (21 votes)
Please Sign up or sign in to vote.
4.80 (21 votes)
4 May 2011CPOL
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:

Image 1
  • 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:

Image 2

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

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

//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)
    { = name;

    void IEmployee.ShowHappiness()
        Console.WriteLine("Employee " + + " showed happiness");

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

    public Consultant(string name)
    { = name;

    protected void ShowSmile()
        Console.WriteLine("Consultant " + + " showed smile");

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

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

Image 3

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


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


About the Author

United States United States
No Biography provided

Comments and Discussions

Questionexcellent article! Pin
GIANGPZO30-Nov-14 3:35
professionalGIANGPZO30-Nov-14 3:35 
SuggestionVery nice work, but this won't work with sealed classes Pin
PRouleau3219-Nov-13 7:32
MemberPRouleau3219-Nov-13 7:32 
GeneralMy Vote of 5 Pin
vishakhakhadse17-Feb-13 16:41
Membervishakhakhadse17-Feb-13 16:41 
GeneralMy vote of 5 Pin
Kanasz Robert25-Jun-12 8:58
MemberKanasz Robert25-Jun-12 8:58 
Questiongreat Pin
Ashraf Hesham8-May-12 10:22
MemberAshraf Hesham8-May-12 10:22 
QuestionNice and Concise Pin
Rajibdotnet053-Apr-12 14:36
MemberRajibdotnet053-Apr-12 14:36 
GeneralMy vote of 5 Pin
Rajibdotnet053-Apr-12 14:35
MemberRajibdotnet053-Apr-12 14:35 
GeneralMy vote of 5 Pin
Jacek Gajek1-May-11 11:13
MemberJacek Gajek1-May-11 11:13 
GeneralImages and links to devlake is broken Pin
meraydin1-May-11 5:55
Membermeraydin1-May-11 5:55 
GeneralMy vote of 5 Pin
raja_krish29-Apr-11 6:05
Memberraja_krish29-Apr-11 6:05 
GeneralMy vote is 5 Pin
Shahriar Iqbal Chowdhury/Galib29-Apr-11 4:59
professionalShahriar Iqbal Chowdhury/Galib29-Apr-11 4:59 
GeneralMy vote of 5 Pin
Santanu Tripathy28-Apr-11 21:03
MemberSantanu Tripathy28-Apr-11 21:03 
GeneralMy vote of 5 Pin
Oshtri Deka26-Apr-11 21:20
professionalOshtri Deka26-Apr-11 21:20 

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

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

Technical Blog
Posted 24 Apr 2011

Tagged as


46 bookmarked