In this article we would like to implement an Adaptor Design Pattern in three ways. This is not the right article to understand the fundamentals of the Adaptor Design Pattern but if you are already familiar with the concept then you can understand the implementation in this article.
Fine, the Adaptor Design Pattern has evolved to communicate to various classes through a single class. The traditional way is to implement a single interface in all classes and from the adaptor class we call the specific function with the help of an instance of an interface.
In the following example, we have implemented the same concept.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
namespace AdaptorTest
{
public interface ILaptop
{
void ShowBrand();
}
public class hp :ILaptop
{
public void ShowBrand()
{
Console.WriteLine("Charging to HP Laptop");
}
}
public class Compaq : ILaptop
{
public void ShowBrand()
{
Console.WriteLine("Charging to compaq Laptop");
}
}
public class laptopAdaptor
{
public static void makeCharge(ILaptop Obj)
{
Obj.ShowBrand();
}
}
class Program
{
static void Main(string[] args)
{
laptopAdaptor.makeCharge(new hp());
laptopAdaptor.makeCharge(new Compaq());
Console.ReadLine();
}
}
}
Here is sample output.
Now, in this example, we will implement the same concept in various ways. Here the “laptopAdaptor” class is a generic class and we have allowed the generic class to create an object of those classes that have implemented an ILpaptop interface.
Have a look at the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
namespace AdaptorTest
{
public interface ILaptop
{
void ShowBrand();
}
public class hp :ILaptop
{
public void ShowBrand()
{
Console.WriteLine("Charging to HP Laptop");
}
}
public class Compaq : ILaptop
{
public void ShowBrand()
{
Console.WriteLine("Charging to compaq Laptop");
}
}
public class laptopAdaptor<T> where T : ILaptop,new()
{
public static void makeCharge()
{
new T().ShowBrand();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Adaptor pattern using Generic Class:\n");
laptopAdaptor<hp>.makeCharge();
laptopAdaptor<Compaq>.makeCharge();
Console.ReadLine();
}
}
}
Let's look closely at the definition of the Main() function. We are calling the makeCharge() function using a class name (as it is a static method) and when it is callied we are passing class name as a parameter of the laptopAdaptor class.
Here is sample output.
We can modify the preceding example a little, in this implementation we will use a generic function rather than a generic class to implement the Adaptor pattern. Have a look at the following example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
namespace AdaptorTest
{
public interface ILaptop
{
void ShowBrand();
}
public class hp :ILaptop
{
public void ShowBrand()
{
Console.WriteLine("Charging to HP Laptop");
}
}
public class Compaq : ILaptop
{
public void ShowBrand()
{
Console.WriteLine("Charging to compaq Laptop");
}
}
public class laptopAdaptor
{
public static void makeCharge<T>() where T: ILaptop,new()
{
new T().ShowBrand();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Adaptor pattern using generic function");
laptopAdaptor.makeCharge<hp>();
laptopAdaptor.makeCharge<Compaq>();
Console.ReadLine();
}
}
}
Here is sample output.
Conclusion
In this article we have learned how to implement an Adaptor Design Pattern in various ways, I hope you have enjoyed the article.