Adapter Pattern

 
  • Structural pattern
  • The intent of this pattern is to convert the interface of the class into another interface the client expects.
  • Two variations – Object Adapter Pattern and Class Adapter pattern
  • Adaptor design pattern helps in not breaking the Solid Principle
    • Open closed principle is not broken
    • Single Responsibility is not broken
  • Also called as wrapper design pattern
  • This design pattern comes into picture when already code is existing. For some reason client cannot change the interface, and so the source. Adapter comes into picture to make it compatible
  • Object Adaptor

    • Holds an instance of adaptee. Modifies the response and sends back to compatible type which client expects.

    Class Adaptor

    • Inherits from the Adaptee.
    • Modifies the response coming from Adaptee
    • Client calls the Adapter through IAdaptee.
				
					

namespace adapterpattern
{
    public interface IAddress
    {
        MyCompatibleAddress GetAddress();
    }

    public class MyCompatibleAddress
    {
        public string Street_LandMark { get;  set; }
        public string State_PinCode { get;  set; }

        public override string ToString()
        {
            return $"{Street_LandMark} :: {State_PinCode}";
        }

    }

    public class ExternalAddress
    {
        public string Street { get;private set; }
        public string Floor { get;private set; }
        public string Landmark { get;private set; }
        public string Pincode { get;private set; }
        public string State { get;private set; }
        public ExternalAddress(string street,string floor, string landmark, string pincode, string state)
        {
            Street = street;
            Floor = floor;
            Landmark = landmark;
            Pincode = pincode;
            State = state;
        }
    }

    public class ExternalSystem
    {
        public ExternalAddress externalAddress()
        {
            return new ExternalAddress("New Colony", "306", "Uday Plaza", "765001", "Odisha");
        }
    }
    public class AddressObjectAdapter : IAddress
    {
        public MyCompatibleAddress GetAddress()
        {
           var addressExternal = new ExternalSystem().externalAddress();
            return new MyCompatibleAddress()
            {
                State_PinCode = addressExternal.State + "-->" + addressExternal.Pincode,
                Street_LandMark = addressExternal.Street + "-->" + addressExternal.Landmark,
            };
        }
    }
}

				
			

1. AddressClassAdapter : ExternalSystem ,IAddress

2. var addressExternal = base.externalAddress();

				
					 public class AddressClassAdapter : ExternalSystem,IAddress
    {
        public MyCompatibleAddress GetAddress()
        {
            var addressExternal = base.externalAddress();
            return new MyCompatibleAddress()
            {
                State_PinCode = addressExternal.State + "==>" + addressExternal.Pincode,
                Street_LandMark = addressExternal.Street + "==>" + addressExternal.Landmark,
            };
        }
    }
				
			
				
					using adapterpattern;

var obj1 = new AddressObjectAdapter();
Console.WriteLine(obj1.GetAddress().ToString());

var obj2 = new AddressClassAdapter();
Console.WriteLine(obj2.GetAddress().ToString());
				
			

New Colony–>Uday Plaza :: Odisha–>765001
New Colony==>Uday Plaza :: Odisha==>765001

Leave a Comment