Adapter Design Pattern in C#

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

Adapter Design Pattern in C# - 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#.

Problem Statement

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.

Class DiagramAdapter Design Pattern in C# - Class Diagram

Adapter Design Pattern in C# – Implementation Code

Result

Adapter Design Pattern in C# - Result

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.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *