Learn Design Pattern - Adapter Pattern

With the previous article about the Prototype Pattern we are done with the creation categories.

Let's move on to the Structural Pattern, which deals with the architectural decisions affecting the structure of a class.


Agenda

  • What is an Adapter Pattern?
  • When to use the Adapter Pattern.
  • Implement an Adapter Pattern in an ASP.Net application.
  • Class Diagram.

Previous Articles

  1. DesignPatterns: Introduction
  2. LearnDesign Pattern - Singleton Pattern
  3. LearnDesign Pattern - Factory Method Pattern
  4. LearnDesign Pattern - Abstract Factory Pattern
  5. LearnDesign Pattern - Builder Pattern
  6. LearnDesign Pattern - Prototype Pattern

What is Adapter Pattern?

  • Acoording to the GOF, an Adapter Pattern converts the interface of a class into another interface that clients expect. An adapter lets classes work together that couldn't otherwise, because of incompatible interfaces. 
  • In simple words, an Adapter Pattern creates a wrapper around an existing class and makes it compatible with another class.

(Don't go into depth, it will be easier once we are done with the sample/example).

When to Use?

Many times it happens, when 2 classes are incompatible with each other due to interfaces.

We can take an example of an electric plug.

The American plug and the British plug are not compatible with each other.

 adp1.JPG   adp2.JPG

American                                       British

Both are of different shape, one is Rectangular and one is cylindrical. Hence both of them need different sockets. The British plug can't be directly used in American sockets.

adp3.JPG

American Socket

We need something like this.

 adp4.JPG

British to American Adapter

Now both plugs can be easily plugged into the American socket.

In other words, both classes (AmericanPlug and BritishPlug) which were incompatible with each other before, are now compatible because of an adapter.

The Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need.

How to Implement the Adapter Pattern in an ASP.Net Application

Consider a company which has 3 kinds of employees (Grade1, Grade2 and Grade 3) and 2 kinds of consultants (Grade1 and Grade2). 

They have an Employee Management System with them.(For Consultants external library is used, means no source code is available for consultants) 

Existing structure

  • Employee

adp5.JPG

  • Consultant
adp6.JPG

The Class diagram indicates that Employee and Consultant are not compatible with each other. So reusing the same code for both employee and consultant is very difficult.

(As there is no common interface between them, in short their interfaces are incompatible with each other so of course they are not).


New Requirement: Now they want to create a UI which will calculate the Final Salary of Employees and Consultants.


Sample Output:

adp7.JPG

Abstract Pattern as solution

The Abstract pattern says that when there are 2 classes, say "Employee" and "Consultant", which are incompatible with each other, a new class will be created "EmployeeAdapter" (which will be compatible with "Employee") as a wrapper around the existing class "Consultant".

Still confused? Let's create an example, as in:

public class G1ConsultantEmployee : G1Consultant,IEmployee
{
          public int GetFinalSalary(int Salary)
          {
                   return this.GetFinalIncome(Salary);
          }
}

public class G2ConsultantEmployee : G2Consultant,IEmployee
{
          public int GetFinalSalary(int Salary)
          {
                   return this.GetFinalIncome(Salary);
          }
}

Client Code:

IEmployee objPriEmployee = new G1Employees();
Response.Write("Final Salary is :" + objPriEmployee.GetFinalSalary(int.Parse(TxtSalary.Text)));
IEmployee objPriEmployee2 = new G1ConsultantEmployee ();
Response.Write("Final Salary is :" + objPriEmployee2.GetFinalSalary(int.Parse(TxtSalary.Text)));


Class Diagram

 Adapter Class Diagram.png

Note: We are not required to change any of the existing code in either G1Employees or G1Consultant. Just by means of inheritance we made 2 of them compatible.

Please download the sample attached for the full code demonstration. In the sample you can see how a single Factory Method Pattern is used to create both Consultants and Employees because of the Adapter Pattern.

Hope you enjoyed reading this article, Feedback and suggestions are always welcome.

Stay tuned for more Structural Patterns.


Just Compile LLP
Just Compile is an IT based firm based out of Mumbai. Application Development, Staffing, Training