C# | Adapter Design Pattern | Structural Design Pattern

Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.

This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.

We are demonstrating use of Adapter pattern via following example in which an audio player device can play mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files.


Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

1.      ITraget - Target interface

2.      Employee Adapter- Adapter Class

3.      HR System- Adaptee Class

4.      ThirdPartyBillingSystem - Client


Code syntax-

/// <summary>

/// The 'Client' class

/// </summary>

public class ThirdPartyBillingSystem

{

    private ITarget employeeSource;

    public ThirdPartyBillingSystem(ITarget employeeSource)

    {

        this.employeeSource = employeeSource;

    }


    public void ShowEmployeeList()

    {

        List<string> employee = employeeSource.GetEmployeeList();

        //To DO: Implement you business logic

        Console.WriteLine("######### Employee List ##########");

        foreach (var item in employee)

        {

            Console.Write(item);

        }

    }

}


/// <summary>

/// The 'ITarget' interface

/// </summary>

public interface ITarget

{

    List<string> GetEmployeeList();

}


/// <summary>

/// The 'Adaptee' class

/// </summary>

public class HRSystem

{

    public string[][] GetEmployees()

    {

        string[][] employees = new string[4][];

        employees[0] = new string[] { "100", "Deepak", "Team Leader" };

        employees[1] = new string[] { "101", "Rohit", "Developer" };

        employees[2] = new string[] { "102", "Gautam", "Developer" };

        employees[3] = new string[] { "103", "Dev", "Tester" };

        return employees;

    }

}


/// <summary>

/// The 'Adapter' class

/// </summary>

public class EmployeeAdapter : HRSystem, ITarget

{

    public List<string> GetEmployeeList()

    {

        List<string> employeeList = new List<string>();

        string[][] employees = GetEmployees();

        foreach (string[] employee in employees)

        {

            employeeList.Add(employee[0]);

            employeeList.Add(",");

            employeeList.Add(employee[1]);

            employeeList.Add(",");

            employeeList.Add(employee[2]);

            employeeList.Add("\n");

        }

        return employeeList;

    }


internal class Program

{

    static void Main(string[] args)

    {

        ITarget Itarget = new EmployeeAdapter();

        ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget);

        client.ShowEmployeeList();

        Console.ReadKey();

    }

}

When to use it?

1.      Allow a system to use classes of another system that is incompatible with it.

2.      Allow communication between new and already existing system which are independent to each other

3.      Ado.Net SqlAdapter, OracleAdapter, MySqlAdapter are best example of Adapter Pattern.

Comments

Popular posts from this blog

C# | SOLID Principles

C# | Design Patterns