Object Oriented Programming Patterns for Geeks

The Adapter Pattern

 

namespace ooppatterns
{
    using System;

    internal class Program
    {
        private static void Main( string[] args )
        {
            ITarget target;

            target = new NewSystem();
            target.Request();

            target = new AdapterToOldSystem();
            target.Request();


            Console.ReadLine();
        }
    }

    interface ITarget
    {
        void Request();
    }

    class NewSystem : ITarget
    {
        public void Request()
        {
            Console.WriteLine( "Do New Operation." );
        }
    }

    // This is sometimes known as a 'wrapper', 
    // although the term wrapper can also be used
    // to describe a facade object.
    class AdapterToOldSystem : ITarget
    {
        private OldLegacyObject adaptee = new OldLegacyObject();

        public void Request()
        {
            adaptee.DoLegacyOperation();
        }
    }

    // Some old warehouse system maybe...
    class OldLegacyObject
    {
        public void DoLegacyOperation()
        {
            Console.WriteLine( "Do Legacy Operation." );
        }
    }
}