Adapter
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:
Future series of articles on sharepoint:
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 see an example. Using the
example given in the composite design pattern, where we had an organization tree that was constructed where all the employees implements 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.
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
}
}
Future series of articles on sharepoint:
- sharepoint list -- the concepts of sharepoint list and how to effectively manage it
sharepoint version control -- the internals of sharepoint version control and how to administer and manage the versions
sharepoint permissions -- how to manage the permissions in a large enterprise sharepoint environment
sharepoint server farm -- how to set up a high availability sharepoint server farm
sharepoint document library -- the details on how to get your ways around the document library in sharepoint
sharepoint configuration -- the configurations needed for different sharepoint network scenarios
sharepoint css -- making the most out of customizing sharepoint frontend
sharepoint web services -- some of the most convienent ways to communicate with the internals of sharepoint