Tuesday, August 18, 2009

Adapter Pattern in C# ( Wrapper Pattern in C# )

Intent:
The adapter pattern is used to allow two incompatible types to communicate, where one class relies upon a specific interface that is not implemented by another class, the adapter acts as a translator between the two classes.

Non-Software Example:
The cell phone adapter can be an example for adapter pattern. Cell phones are normally designed to take 9 Volts of DC (Direct Current).The normal power supply at home (in India) is 220 Volts of AC (Alternate Current). So if we say the cell phone is one class and power supply at home is another class then power supply can not be used directly for the cell phone. Hence we need an adapter for the cell phone to get the cell phone charged by the normal power supply at home.



Benefits:
The adapter design pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.

UML


The UML class diagram above describes an implementation of the adapter design pattern. The items in the diagram are described below:

 Client. The client class requires to use an incompatible class. It expects to interact with a type that implements the ITarget interface. However, the class that we wish it to use is the incompatible Adaptee.
 ITarget. This is the expected interface for the client class. Although shown in the diagram as an interface, it may be a class that the adapter inherits. If a class is used, the adapter must override its members.
 Adaptee. This class contains the functionality that is required by the client. However, its interface is not compatible with that which is expected.
 Adapter. This class provides the link between the incompatible Client and Adaptee classes. The adapter implements the ITarget interface and contains a private instance of the Adaptee class. When the client executes MethodA on the ITarget interface, MethodA in the adapter translates this request to a call to MethodB on the internal Adaptee instance.

Software Example & Explanation of Code:

Let us assume that we are building a web application for a school. One of the requirements is to display all the teacher’s details along with the school information. We already have a class (TeachersClass.cs) which stores the teacher information. The other school information course offered, current student strength, Fees etc is stored in the “SchoolDetails.cs” class. The client (Default.aspx) wants all the inform stored in both “TeachersClass.cs” and “SchoolDetails.cs” class. Since these two classes are not compatible to each other we have build the “SchoolAdapter.cs” class which will make the communication between these two classes. Let us take each of the classes at a time,

Note : The example illustrated here is to make the concept clear and understandable. This code cannot be directly used on production system to apply adapter pattern.


TeachersDetails.cs

The teachersDetails class has all the teachers information.

public class TeachersDetails
{
public string[][] GetTeachers()
{
string[][] teachers = new string[4][];

teachers[0] = new string[] { "1221","Shyam","Science Teacher" };
teachers[1] = new string[] { "3233","Ram", "Computer Teacher" };
teachers[2] = new string[] { "4322","Laxman","History Teacher" };
teachers[3] = new string[] { "1234","Mohan","Biology Teacher" };

return teachers;
}

}

SchoolDetails.cs

The SchoolDetails class has all the required information about the school, but not the teachers information.

public class SchoolDetails
{
ITeacherDetails _objTeacherDetails;

public SchoolDetails(ITeacherDetails objTeacherDetails)
{
_objTeacherDetails = objTeacherDetails;
}

public string getTeacherString()
{
return _objTeacherDetails.GetTeachers();
}
public string GetStudentStrenght()
{
return "Class 1 = 50 studernt
Class 2 = 50
";
}
public string GetCourseOffered()
{
return "Package with Maths
Package with Biology
";
}
public string GetCourseFee()
{
return "Package with Maths : $400
Package with Biology : $500
";
}
}
public interface ITeacherDetails
{
string GetTeachers();
}

SchoolAdapter.cs

The SchoolAdapter.cs class is the adapter class which does the communication between the SchoolDetails class and the TeacherDetails class.

public class SchoolAdapter:ITeacherDetails
{
TeachersDetails _objTeacherDetails;
public SchoolAdapter(TeachersDetails objTeacherDetails)
{
_objTeacherDetails = objTeacherDetails;
}
public string GetTeachers()
{
string[][] objTeacher = _objTeacherDetails.GetTeachers();
StringBuilder objString = new StringBuilder();

foreach(string[] Teacher in objTeacher)
{
objString.Append("Name of Teacher :" + Teacher[1]+"--");
objString.Append("Subject :" + Teacher[2] + "--");
objString.Append("Extn" + Teacher[0] + "--");
objString.Append("==============================" + "--");
}
return objString.ToString();
}

}

The entire code for download is available at http://www.csharppatterns.com/


No comments: