You can see this and other great articles on design patterns here.

The Adapter Design Pattern allows you to make an existing class work with other existing class libraries without changing the code of the existing class.

We often need to use the methods of an existing class to work with other existing libraries. The way to do this is by creating another class, named the Adapter, that inherits from the existing class while implementing the interface of the existing library. The end result is that the Adapter can call the method of the existing class (since the Adapter inherits from the existing class) and can work in the existing library (since the Adapter implements the interface of the existing library).

Below is the UML of the Adapter Design Pattern:

  • The Adaptee is the existing class.
  • The IInterface is the interface defined in the existing library.
  • The Adapter is the class that you create, it is inherited from the Adaptee class and it implements the IInterface interface. Notice that it can call the OperationA method(inherited from the Adaptee) inside its OperationB method (implemented by the IInterface).

Let's do an example. Using the example given in the composite design pattern, where we had an organization tree that was constructed where all the employees implement the IEmployee interface. The IEmployee interface is from the existing library.

You are then given the Consultant class and you need to plug this Consultant class into the organization tree. The Consultant class is the Adaptee.

The way to do this is by creating the adapter class named the EmployeeAdapter, that inherits from the Consultant class while it implements the IEmployee interface:

In the Adapter, we can then call the methods from the parent class to mimic the behaviors needed in the common interface. In the EmployeeAdapter class, we can call the ShowSmile method from the parent Consultant class in its implementation of the IEmployee interface, which requires the ShowHappiness method. 

 

Below are the implementation code and the output of the Adapter Design Pattern from the example. Notice that we don't need to change the existing code in the Consultant class, yet we can plug its functionality into the existing IEmployee interface:

using System.Collections.Generic;    

class Program
{
    static void Main(string[] args)
    {
        List<iemployee> list = new List<iemployee>();
        list.Add(new Employee("Tom"));
        list.Add(new Employee("Jerry"));
        list.Add(new EmployeeAdapter("Bruno"));  //consultant from existing class
        ShowHappiness(list);
    }

    //*** Code below from the existing library does not need to be changed ***
    static void ShowHappiness(List<iemployee> list)
    {
        foreach (IEmployee i in list)
            i.ShowHappiness();
    }
}

//from the existing library, does not need to be changed
public interface IEmployee
{
    void ShowHappiness();
}

public class Employee : IEmployee
{
    private string name;
    
    public Employee(string name)
    {
        this.name = name;
    }

    void IEmployee.ShowHappiness()
    {
        Console.WriteLine("Employee " + this.name + " showed happiness");
    }
}

//existing class does not need to be changed
public class Consultant
{
    private string name;

    public Consultant(string name)
    {
        this.name = name;
    }

    protected void ShowSmile()
    {
        Console.WriteLine("Consultant " + this.name + " showed smile");
    }
}

public class EmployeeAdapter : Consultant, IEmployee
{
    public EmployeeAdapter(string name) : base(name)
    {
    }

    void IEmployee.ShowHappiness()
    {
        base.ShowSmile();  //call the parent Consultant class
    }
}

Liked this article? You can see this and other great articles on design patterns here.

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"