Skip to content

Adapter Design Pattern

Converts the [[interface]] of a [[class]] into another [[interface]] clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Adapter exampleΒΆ

namespace SOLID.ISP
{
    public interface IWideInterface
    {
        void A();
        void B();
        void C();
        void D();
    }

    public interface INarrowInterface
    {
        void A();
        void B();
    }

    class Adapter : INarrowInterface
    {
        private readonly IWideInterface _wide;

        public Adapter(IWideInterface wide)
        {
            _wide = wide;
        }

        public void A()
        {
            _wide.A();
        }

        public void B()
        {
            _wide.B();
        }
    }

    class Client
    {
        private readonly INarrowInterface _narrow;

        public Client(INarrowInterface narrow)
        {
            _narrow = narrow;
        }
    }
}