Adaptor design Pattern
It consiste of a class which acts a a bridge between two different and independent classes.
When an interface is not compatible with a class , An adapter is used to make it compatible
customer_list is a class which contains employee list in two dimensional string array.
the client class accept employee name in array list . The adaper design pattern becomes
handy in such scenario but doing the required conversion of the two dimensional string
array to list of name for the employees.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Liskov Substitution Principle
namespace ManualResetEvent_
{
// Adapter class//
class customer_list
{
public string[][] customer;
public string[][] getCustomerNameList()
{
customer = new string[5][];
customer[0] = new string[] { "100", "Amit", "INDIA" };
customer[1] = new string[] { "200", "Arun", "INDIA" };
customer[2] = new string[] { "300", "Arup", "INDIA" };
customer[3] = new string[] { "400", "Ajit", "INDIA" };
customer[4] = new string[] { "500", "Ajay", "INDIA" };
return customer;
}
}
class AdapterClass : ITarget
{
public List<string> getCustomerList()
{
customer_list cus = new customer_list();
List<string> customerlist = new List<string>();
string[][] employee=cus.getCustomerNameList();
foreach (string[] cstmr in employee)
{
customerlist.Add(cstmr[1]);
}
return customerlist;
}
}
//Interface //
interface ITarget
{
List<string> getCustomerList();
}
//Main Class //
class shoppingmall
{
static ITarget t;
public static void ShowEmployeeList()
{
t=new AdapterClass();
t.getCustomerList().ForEach(x=> Console.WriteLine(x.ToString()));
}
public static void Main()
{
ShowEmployeeList();
Console.ReadLine();
}
}
}
When an interface is not compatible with a class , An adapter is used to make it compatible
customer_list is a class which contains employee list in two dimensional string array.
the client class accept employee name in array list . The adaper design pattern becomes
handy in such scenario but doing the required conversion of the two dimensional string
array to list of name for the employees.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Liskov Substitution Principle
namespace ManualResetEvent_
{
// Adapter class//
class customer_list
{
public string[][] customer;
public string[][] getCustomerNameList()
{
customer = new string[5][];
customer[0] = new string[] { "100", "Amit", "INDIA" };
customer[1] = new string[] { "200", "Arun", "INDIA" };
customer[2] = new string[] { "300", "Arup", "INDIA" };
customer[3] = new string[] { "400", "Ajit", "INDIA" };
customer[4] = new string[] { "500", "Ajay", "INDIA" };
return customer;
}
}
class AdapterClass : ITarget
{
public List<string> getCustomerList()
{
customer_list cus = new customer_list();
List<string> customerlist = new List<string>();
string[][] employee=cus.getCustomerNameList();
foreach (string[] cstmr in employee)
{
customerlist.Add(cstmr[1]);
}
return customerlist;
}
}
//Interface //
interface ITarget
{
List<string> getCustomerList();
}
//Main Class //
class shoppingmall
{
static ITarget t;
public static void ShowEmployeeList()
{
t=new AdapterClass();
t.getCustomerList().ForEach(x=> Console.WriteLine(x.ToString()));
}
public static void Main()
{
ShowEmployeeList();
Console.ReadLine();
}
}
}
Comments
Post a Comment