Before starting, I would request that you visit the previous articles on Design Patterns series:
- Design Patterns Simplified: Part 1
- Design Patterns Simplified - Part 2 (Singleton)
- Design Patterns Simplified - Part 3 (Simple Factory)
- Design Patterns Simplified - Part 4 (Abstract Factory)
- Design Patterns Simplified - Part 5 (Factory Method)
- Design Patterns Simplified - Part 6 (Prototype)
- Design Patterns Simplified - Part 7 (Builder)
- Design Patterns Simplified - Part 8 (Facade)
I am here to continue the discussion around Design Patterns. Today we will go through one of the structural design patterns called Adapter. Before talking about its implementation let’s begin with defining it.
As per GOF guys, Adapter pattern is defined as the following.
“Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.”
Well! Let’s understand what they mean.
It means that Adapter pattern links two incompatible interfaces which have the same functionality but their methods are called a little differently. Sometimes it is also called a wrapper.
How Adapter pattern works
Adapter pattern can be used to integrate with other external components that might have the same functionality we need but their functions are not called quite the same way. Assume we have an Employee class with a function defined as PrintEmployee and we work with an external contract employee provider that has an EmployeeDetail class with a function as ListEmployee.
Here we can create an adapter class to wrap the external EmployeeDetailclass so that a call to PrintEmployee() of the adapter is delegated to ListEmployee() of the external class.
Let’s understand this by a simple example.
- ///<summary>
- ///Adaptee Class
- ///</summary>
- public class EmployeeDetail
- {
- //// Code below from the existing library does not need to be changed.
- public static void ListEmployee(List < string > empList)
- {
- foreach(stringempinempList)
- {
- Console.WriteLine("Employee Name: " + emp);
- }
- }
- }
- ///<summary>
- /// The 'ITarget' interface
- ///</summary>
- public interface IEmployee
- {
- voidPrintEmployee(List < string > empList);
- }
- ///<summary>
- /// The 'Adapter' class
- ///</summary>
- public class EmployeeAdapter: IEmployee
- {
- public void PrintEmployee(List < string > empList)
- {
- EmployeeDetail.ListEmployee(empList);
- }
- }
- staticvoid Main()
- {
- Console.Title = "Adapter pattern demo";
- //Client setup
- List < string > empList = newList < string > ();
- empList.Add("Prakash");
- empList.Add("Amit");
- empList.Add("Pankaj");
- IEmployeeemp = newEmployeeAdapter();
- emp.PrintEmployee(empList);
- }

Here we can see that with the help of EmployeeAdapter class, client is able to print the employee records.
So in summary, adapter pattern can be used when the current system needs to use functions of another system that is incompatible with it. Some of the most popular examples can be SqlAdapter, OracleAdapter, MySqlAdapter in ADO.NET.

Rajan MishraPosted Oct 17, 2018, 2:23 AM
Hey Prakash can you please tell what is the advantage of adapter pattern in the real world.
Prakash TripathiPosted Apr 11, 2016, 3:43 AM
Thnx Humayun.
Humayun Kabir MamunPosted Apr 11, 2016, 3:42 AM
Nice...
Prakash TripathiPosted Apr 9, 2016, 10:42 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:37 AM
Nice article..
Prakash TripathiPosted Mar 12, 2016, 4:04 AM
Thnx Kashif.
Prakash TripathiPosted Mar 12, 2016, 4:04 AM
Thnx Vignesh.
Kashif SohailPosted Mar 11, 2016, 9:26 PM
Nice
Vignesh ManiPosted Mar 11, 2016, 5:44 PM
good
Prakash TripathiPosted Mar 11, 2016, 10:23 AM
Thnx Saillesh.
Saillesh PawarPosted Mar 11, 2016, 9:59 AM
Nice Prakash Sir
Prakash TripathiPosted Mar 11, 2016, 8:01 AM
Thnx Lalit.
Lalit RaghavPosted Mar 11, 2016, 7:20 AM
Nice Article
Prakash TripathiPosted Mar 11, 2016, 5:07 AM
Thnx Sibeesh.
Sibeesh VenuPosted Mar 11, 2016, 4:46 AM
Nice Share
Prakash TripathiPosted Mar 11, 2016, 12:09 AM
Thnx Debasis.
Debasis SahaPosted Mar 10, 2016, 11:51 PM
Nice one..
Prakash TripathiPosted Mar 10, 2016, 9:34 PM
Thnx Asfend.
Asfend YarPosted Mar 10, 2016, 1:54 PM
good