Adapter Design Patterns

Adapter Design Pattern

The adapter pattern converts the interface of a class into another interface clients expect. The adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Real-World Example

• Karthick from India arrived in the UK.
• Karthick has a classic EU charger with a two-part outlet.
• The UK has a standard three-part plug.

How will Karthick charge his phone?

Answer- By using an adapter.

Adapter

The adapter creates the possibility of connecting two different interfaces (2 plugs). It gets one type of input and produces a different type of output.

When to use?

  • Ado .Net SqlAdapter, MySqlAdapter
  • Allow a system to use classes of another system that is incompatible with it.
    public interface ITarget
    {
        string GetRequest();
    }

    class Adadptee
    {
        public string GetSpecificRequest()
        {
            return "Specific request";
        }
    }

    class Adapter : ITarget
    {
        private readonly Adadptee _adadptee;

        public Adapter(Adadptee adadptee)
        {
            this._adadptee = adadptee;
        }

        public string GetRequest()
        {
            return $"this is '{this._adadptee.GetSpecificRequest()}";
        }
    }

    static void Main(string[] args)
        {
            Adadptee adaptee = new Adadptee();
            ITarget target = new Adapter(adaptee);
            Console.WriteLine(target.GetRequest());
        }
Next Recommended Reading Design Patterns in C#