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 16, 2018, 10:23 PM EDT
Hey Prakash can you please tell what is the advantage of adapter pattern in the real world.
Prakash TripathiTOP 100Posted Apr 10, 2016, 11:43 PM EDT
Thnx Humayun.
Humayun Kabir MamunPosted Apr 10, 2016, 11:42 PM EDT
Nice...
Prakash TripathiTOP 100Posted Apr 9, 2016, 6:42 AM EDT
Thnx Nagaraj.
Kuppurasu NagarajTOP 100Posted Apr 9, 2016, 6:37 AM EDT
Nice article..
Prakash TripathiTOP 100Posted Mar 11, 2016, 11:04 PM EST
Thnx Kashif.
Prakash TripathiTOP 100Posted Mar 11, 2016, 11:04 PM EST
Thnx Vignesh.
Kashif SohailPosted Mar 11, 2016, 4:26 PM EST
Nice
Vignesh ManiTOP 100Posted Mar 11, 2016, 12:44 PM EST
good
Prakash TripathiTOP 100Posted Mar 11, 2016, 5:23 AM EST
Thnx Saillesh.
Saillesh PawarPosted Mar 11, 2016, 4:59 AM EST
Nice Prakash Sir
Prakash TripathiTOP 100Posted Mar 11, 2016, 3:01 AM EST
Thnx Lalit.
Lalit RaghavPosted Mar 11, 2016, 2:20 AM EST
Nice Article
Prakash TripathiTOP 100Posted Mar 11, 2016, 12:07 AM EST
Thnx Sibeesh.
Sibeesh VenuTOP 100Posted Mar 10, 2016, 11:46 PM EST
Nice Share
Prakash TripathiTOP 100Posted Mar 10, 2016, 7:09 PM EST
Thnx Debasis.
Debasis SahaTOP 100Posted Mar 10, 2016, 6:51 PM EST
Nice one..
Prakash TripathiTOP 100Posted Mar 10, 2016, 4:34 PM EST
Thnx Asfend.
Asfend YarPosted Mar 10, 2016, 8:54 AM EST
good