.NET Articles

Adapter Design Pattern in .Net


The adapter class works as a bridge between Interface and main functionality class.

Client makes a request to Interface. Adapter class implements the interface and inherits from the main class. Adapter class makes calls to main class and provides result to the client.

Note: "Main class" denotes the class which has main functionality that client is expecting.

This design pattern falls under Structural Patterns.

Following are the classes or interfaces participating in this pattern:

ITarget

Client makes call to this interface to achieve the purpose.

Adapter

This class implements the ITarget interface and inherit from the Adaptee class. This works as a mediator between ITarget interface and Adaptee class.

Adaptee

This class contains the main functionality that client is expecting.

Client

This class is used for communicate with the Adapter class that implements the ITarget interface. In other words, this is the class used for creating the instance of Adapter class.

C# example for Adapter design pattern

In example, class and interface details mapped to parts of Adapter design pattern.

ITarget :IStaff

Adapter :StaffAdapter

Adaptee :Staff

Client :Program


using System.Collections.Generic;
using System;


//Client
class Program
{
    static void Main(string[] args)
    {
        IStaff _staff = new StaffAdapter();
        _staff.DisplayEmployees();
        _staff.AddEmployee("04", "Sam", 4000);
        Console.ReadLine();    
    }    
}

//  ITarget interface
public interface IStaff
{
     void DisplayEmployees();
     void AddEmployee(string empID, string empName, int salary);
}

//  Adapter class
public class StaffAdapter :Staff,IStaff
{
    public void DisplayEmployees()
    {
        List<Employee> _empList = GetAllEmployees();
        foreach (Employee emp in _empList)
        {
            Console.WriteLine("ID =" + emp.EmpID + ", Name =" + emp.EmpName + ", Salary =" + emp.Salary.ToString());
        }
    }
    public override void AddEmployee(string empID, string empName, int salary)
    {
        base.AddEmployee(empID, empName, salary);
    }
}

//  Adaptee class
public class Staff
{
    List<Employee> _epmployeeCollection = new List<Employee>();
    public List<Employee> Employees
    {
        get { return _epmployeeCollection; }
    }

    public List<Employee> GetAllEmployees()
    {
        _epmployeeCollection.Add(new Employee("001", "Ram", 1000));
        _epmployeeCollection.Add(new Employee("002", "Gopal", 2000));
        _epmployeeCollection.Add(new Employee("003", "Sam", 3000));
        return Employees;
    }

    public virtual void AddEmployee(string empID, string empName, int salary)
    {
        Employees.Add(new Employee(empID, empName, salary));
        Console.WriteLine();
        Console.WriteLine("New employee added successfully.");        
    }
}

public class Employee
{
    public  Employee(string empID, string empName, int salary)
    {
        this.EmpID = empID;
        this.EmpName = empName;
        this.Salary = salary;
    }
    public string EmpID { get; set; }
    public string EmpName { get; set; }
    public int Salary { get; set; }
}
        

Output:

Adapter design pattern