Adapter design pattern


DESIGN PATTERS - Structural Patterns - Adapter Pattern with Introduction, Steps, UMLS, Complete Code and Test Client.


DESIGN PATTERS - Structural Patterns - Adapter Pattern

## Environment: Ms Windows XP Prof 2002 (SP2) & Visual Studio 2008 ##

INTRODUCTION

When 2 classes implements the difference interface yet need to communicate each other. In such scenarios, Adopter pattern is used:

STEPS - Implementation...

a.) Identify the Adaptee (A legacy class)
b.) Create an Adapter interface (Declare methods required by the new class)
c.) Create an Adapter class (Class that implements the interface)
d.) Identify the Client Class (Class that needs functionalities declared in Adapter interface)
e.) How to use this implementation in a test client (see below)...
e.1) Create instance of Adaptor class
e.2) Create instance of Client class and pass instance of Asaptor class in its constructor
e.3) call a methods of client that inturn call Adaptor's methods

Start with a fresh Console application > Name it AdapterPattern > Create following individual classes in it.

SAMPLE - PROTOTYPE FOR ADAPTER PATTERN

a.) Adaptee class



// 1. This is a unknown type for the Client Class
// 2. Adapter class will implement interface that would be mapped to Adapter's methods

public class AdapteeClass
{
public string SomeMethod()
{
return "Message written in Adaptee.SomeMethod()";
}

public string EmployeeName(int employeeCode)
{
// Queries Employee's Full Name based on employeeCode and returns to caller
return "Sharad Kapil Sharma";
}

public string DepartmentName(int departmentCode)
{
// Queries Department Name based on departmentCode and returns
return "Apps";
}

}



b.) Adapter Interface



// 1. This has some methods which are known by the Client
// 2. These methods will be implemented in Adapter and may or may not map wth Adaptee's Methods
// 3. Adaptee is totally isolated from the Client for Type Name and Members

public interface IInterface
{
string SomeMappingMethod();
string EmployeeFullName(int eCode);
string DepartmentDescription(int dCode);
string GetEmployeeDetail(int eCode, int dCode);
}




c.) Adapter Class



// 1. This works as Adapter between Client and Adaptee
// 2. It holds a private reference to Adaptee
// 3. It creates custom constructer that receive & save Adapter instance in a private reference
// 4. It implementes interface that has methods known to Client
// 5. These methods calls Adaptee's methods directly or after manipulations & returns results to Client

public class AdapterClass : IInterface
{
private AdapteeClass referenceOfAdaptee;

public AdapterClass()
{
// Create instance of Adaptee
this.referenceOfAdaptee = new AdapteeClass();
}

#region IInterface Members

public string SomeMappingMethod()
{
// Direct method Call
return referenceOfAdaptee.SomeMethod();
}

public string EmployeeFullName(int eCode)
{
// Direct method Call
return referenceOfAdaptee.EmployeeName(eCode);
}

public string DepartmentDescription(int dCode)
{
// Direct method Call
return referenceOfAdaptee.DepartmentName(dCode);
}

public string GetEmployeeDetail(int eCode, int dCode)
{
string eName = referenceOfAdaptee.EmployeeName(eCode);
string dName = referenceOfAdaptee.DepartmentName(dCode);
return "Full Name is " + eName + " and Department Name is " + dName;
}

#endregion
}



d.) Client Class



// 1. This class doesn't know Adaptee but needs to access some of its methods.
// 2. It create reference to the Interface that has required methods
// 3. Its custom constructor receives & stores Adapter instance into private variable
// 4. It makes calls to Adapter's methods (that internally call Adaptee's methods)

public class ClientClass
{
private IInterface reference;

public ClientClass(IInterface reference)
{
this.reference = reference;
}

public void SomeClientMathod()
{
Console.WriteLine("Message displayed in Client: " + reference.SomeMappingMethod());
}

public void SomeOtherMethods()
{
Console.WriteLine("Employee Full name = " + reference.EmployeeFullName(2052));
Console.WriteLine("Department name = " + reference.DepartmentDescription(1059));
Console.WriteLine("Employee detail = " + reference.GetEmployeeDetail(2052, 1059));
}
}



e.) Test Application



class Program
{
static void Main(string[] args)
{

// Create instance of Adapter
AdapterClass adapter = new AdapterClass();

// Create instance of client and pass Adapter's instance to its constructor
ClientClass client = new ClientClass(adapter);

// Make calls as desired
client.SomeClientMathod();
client.SomeOtherMethods();
Console.ReadKey();
}
}



Compile and test it

Output

Refer : OUTPUT - Adapter Pattern

I hope, in this article, I am able to give some insights to Adapter Pattern. (If any query, please write)

Regards.


Comments

Guest Author: stergiazotali20 Oct 2012

I have tried to understand Adapter design pattern by reading many articles , yet something was missing , that prevented me from fully understand it. This article was that missing 'smtg'

Thanks a lot.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: