Adapter Pattern

Adapter Pattern

It comes under structural type design pattern. It is used for making communication between two incompatible classes. It is used to provide a link between two incompatible classes.

The participants in this pattern are,

  • Target
    it defines domain specific interface which is used by client.

  • Adaptee
    It defines an existing interface that needs adapting; i.e., it is the class which contains the functionality, required by the client. However, its interface is not compatible with the client.

  • Adapter
    It is the class which implements target and inherits Adaptee class. It plays the role of communicator between Client and Adaptee.

  • Client
    It collaborates with objects conforming to the target interface; i.e., this class interacts with a class which implements the target interface. However, the communication class called Adaptee, is not compatible with the client.public class Client {  
    1.     private ITarget target;  
    2.     public Client(ITarget target) {  
    3.         this.target = target;  
    4.     }  
    5.     public void MakeRequest() {  
    6.         target.Method_I();  
    7.     }  
    8. }  
    9. public interface ITarget {  
    10.     void Method_I();  
    11. }  
    12. public class Adapter: Adaptee, ITarget {  
    13.     public void Method_I() {  
    14.         Method_II();  
    15.     }  
    16. }  
    17. public class Adaptee {  
    18.     public void Method_II() {  
    19.         Console.WriteLine("Method_II() is called");  
    20.     }  
    21. }  

Example

The 'Client' class

  1. public class BillingSystem {  
  2.     private ITarget employeeSource;  
  3.     public BillingSystem(ITarget employeeSource) {  
  4.         this.employeeSource = employeeSource;  
  5.     }  
  6.     public void ShowEmployeeList() {  
  7.         List < string > employee = employeeSource.GetEmployeeList();  
  8.         //To DO: Implement you business logic  
  9.         Console.WriteLine("######### Employee List ##########");  
  10.         foreach(var item in employee) {  
  11.             Console.Write(item);  
  12.         }  
  13.     }  
  14. }  
The 'ITarget' interface

 

  1. public interface ITarget {  
  2.     List < string > GetEmployeeList();  
  3. }  

 

The 'Adaptee' class

  1. public class HRSystem {  
  2.     public string[][] GetEmployees() {  
  3.         string[][] employees = new string[4][];  
  4.         employees[0] = new string[] {  
  5.             "100",  
  6.             "Deepak",  
  7.             "Team Leader"  
  8.         };  
  9.         employees[1] = new string[] {  
  10.             "101",  
  11.             "Rohit",  
  12.             "Developer"  
  13.         };  
  14.         employees[2] = new string[] {  
  15.             "102",  
  16.             "Gautam",  
  17.             "Developer"  
  18.         };  
  19.         employees[3] = new string[] {  
  20.             "103",  
  21.             "Dev",  
  22.             "Tester"  
  23.         };  
  24.         return employees;  
  25.     }  
  26. }  

The 'Adapter' class

  1. public class EmployeeAdapter: HRSystem, ITarget {  
  2.     public List < string > GetEmployeeList() {  
  3.         List < string > employeeList = new List < string > ();  
  4.         string[][] employees = GetEmployees();  
  5.         foreach(string[] employee in employees) {  
  6.             employeeList.Add(employee[0]);  
  7.             employeeList.Add(",");  
  8.             employeeList.Add(employee[1]);  
  9.             employeeList.Add(",");  
  10.             employeeList.Add(employee[2]);  
  11.             employeeList.Add("\n");  
  12.         }  
  13.         return employeeList;  
  14.     }  
  15. }  

 

Adapter Design Pattern Demo 

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         ITarget Itarget = new EmployeeAdapter();  
  4.         BillingSystem client = new BillingSystem(Itarget);  
  5.         client.ShowEmployeeList();  
  6.         Console.ReadLine();  
  7.     }  
  8. }  
Ebook Download
View all
Learn
View all