Share via


Adapter Pattern

Adapter pattern converts one instance of a class into another interface which client expects. In other words, Adapter pattern actually makes two classes compatible.

http://1.bp.blogspot.com/_DSP5FXX4Isw/S_RPvFQGH8I/AAAAAAAAC9U/jFhPMXCovlY/s320/adapter.JPG

public interface  IAdapter
    {
        /// <summary>
        /// Interface method Add which decouples the actual concrete objects
        /// </summary>
        void Add();
    }
    public class  MyClass1 : IAdapter
    {
        public void  Add()
        {
        }
    }
    public class  MyClass2
    {
        public void  Push()
        {
 
        }
    }
    /// <summary>
    /// Implements MyClass2 again to ensure they are in same format.
    /// </summary>
    public class  Adapter : IAdapter 
    {
        private MyClass2 _class2 = new MyClass2();
 
        public void  Add()
        {
            this._class2.Push();
        }
    }

Here in the structure, the adapter is used to make MyClass2 incompatible with IAdapter.