mercredi 3 avril 2019

Why use Adapter design pattern

So I am looking at the Adapter design pattern. I see that the intention is to allow a client to access the methods of a class where their interfaces are incompatible.

Now I have been looking at this example.

interface ITarget
{
  List<string> GetProducts();
}


public class VendorAdaptee
{
   public List<string> GetListOfProducts()
   {
      List<string> products = new List<string>();
      products.Add("Gaming Consoles");
      products.Add("Television");
      products.Add("Books");
      products.Add("Musical Instruments");
      return products;
   }
}

class VendorAdapter:ITarget
{
   public List<string> GetProducts()
   {
      VendorAdaptee adaptee = new VendorAdaptee();
      return adaptee.GetListOfProducts();
   }
}

class ShoppingPortalClient
{
   static void Main(string[] args)
   {
      ITarget adapter = new  VendorAdapter();
      foreach (string product in adapter.GetProducts())
      {
        Console.WriteLine(product);
      }
      Console.ReadLine();
   }
}

So based on the description ShoppingPortalClient wants to use VendorAdaptee but can't due to incompatible interface. Here is my stupid question...why can't ShoppingPortalClient just do this:

var adaptee = new VendorAdaptee();

Aucun commentaire:

Enregistrer un commentaire