This article explains what is Adapter pattern and how to implement Adapter design pattern in C#.
Adapter design pattern falls under structural design pattern category of Gang of four (GoF) design patterns. Suppose you want to use different class within your application but their interfaces are not compatible; adapter pattern will be useful in this scenario. Adapter design pattern enables a system to use classes of another system that is incompatible with each other.
What is Adapter Design Pattern?
Adapter design pattern allows a system to use classes whose interfaces are mutually incompatible. Adapter design pattern provides the bridge or connection between these mutually incompatible interfaces. This pattern is useful for off-the-self code, toolkits and libraries.
Gang of Four (GoF) definition:
Convert the interface of a class into another interface client expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Adapter Design Pattern – UML Diagram
Client and Adaptee classes are incompatible with each other. Client calls ITarget interface to achieve functionality of Adaptee class.
Participants in above UML diagram are:
Client: This is the class which is incompatible with Adaptee class but wants to use Adaptee class code
ITarget: The interface that client class uses to achieve Adaptee class functionality.
Adapter: This class implements ITarget interface; this class also calls Adaptee class functionality inside.
Adaptee: This is the class which Client class want to use.
Adapter Design Pattern in C# – Example
Let’s implement a simple example to understand adapter design pattern in C#.
Let’s think of a company (client) which manages records of employees for different third party organizations. These third party organizations have some system (class) to maintain records of their employees. Now you have to write code for client company to get employee records for these third party organizations but client’s interface is not compatible with third party organization’s interface. Here adapter pattern will be useful to get the employee list.
Adapter Design Pattern in C# – Implementation Code
// 'Adaptee' class
public List<string> GetEmployeeList()
List<string> EmployeeList = new List<string>();
// 'ITarget' interface
// 'Adapter' class
class EmployeeAdapter : ThirdPartyEmployee, ITarget
public List<string> GetEmployees()
// 'Client' class
static void Main(string args)
Console.WriteLine("Employee List from ThridPartyEmployee class");
// client will use ITarget interface to call functionality of Adaptee class i.e. ThirdPartyEmployee
ITarget adapter = new EmployeeAdapter();
foreach (string employee in adapter.GetEmployees())
Hope you get the idea about how to implement Adapter design pattern in C#. Please post your comments for queries and feedback. Thanks for reading this article.
Latest posts by Gyanendu Shekhar (see all)
- Create fake shadow using blob shadow projector: Unity tutorial - October 2, 2018
- Using Reflection Probe: Unity Tutorial - October 1, 2018
- Difference Between Material and Shared Material: Unity Tutorial - September 23, 2018