Adapter Design Pattern

The adapter pattern is an extremely useful pattern for a few reasons. The reason I like it so much is because if you understand it, then you understand the concept of Dependency Inversion (the "D" in SOLID object-oriented design).

Dependency Inversion is an object-oriented principle which states that in code, higher level modules should not depend on lower level modules. Instead, lower level modules should depend on higher level ones. Let's say you've got one higher level object that requires a lower level object to do it's job. But the lower-level object is likely to change or be swapped out for another. Here's the bad way to do it:

namespace HigherLevelModule
{
    public class HigherLevelObject
    {
        ......

        public void Run()
        {
            LowerLevelObject obj = new LowerLevelObject();
            
            string result = obj.GetSomething();
            Console.WriteLine(result);
        }
    }
}

namespace LowerLevelModule
{
    public class LowerLevelObject
    {
        ......

        public string GetSomething()
        {
            return new String("you got me!");
        }
    }
}



Now, the reason that this is bad, is because it makes the task of swapping out the lower level object more difficult than it needs to be. The higher level depends on the lower level! LowerLevelModule can be compiled without HigherLevelModule but not vice versa - which is the opposite of what we want.

So here's a better version:

namespace HigherLevelModule
{
    public class HigherLevelObject
    {
        ......

        public void Run()
        {
            IAdapter obj = LowerLevelObjectFactory
                                .GetInstance()
                                .GetLowerLevelObject();
            
            string result = obj.GetSomething();
            Console.WriteLine(result);
        }
    }

    public interface IAdapter
    {
        string GetSomething();
    }

    public class LowerLevelObjectFactory
    {
        private static LowerLevelObjectFactory Factory =
                                new LowerLevelObjectFactory();
        
        private LowerLevelObjectFactory()
        {
        }

        public static LowerLevelObjectFactory GetInstance()
        {
            return Factory;
        }

        public IAdapter GetLowerLevelObject()
        {
            return new LowerLevelObjectA();
        }

    }
}

namespace LowerLevelModule
{
    public class LowerLevelObjectA : IAdapter
    {
        ......

        public string GetSomething()
        {
            return new String("you got A!");
        }
    }

    public class LowerLevelObjectB : IAdapter
    {
        ......

        public string GetSomething()
        {
            return new String("this time you got B!");
        }
    }
}

With this new code, the higher level doesn't care what the actual type of the ILowerLevel instance is, it just cares that it has a GetSomething() method, which is guaranteed by the ILowerLevel contract. Now the higher level can use the factory method to create lower level objects all over the place, and if the type of the lower level object ever has to be changed, there's only one line of code that needs to be changed:

public IAdapter GetLowerLevelObject()
{
    return new LowerLevelObjectB();
}


Now THAT's maintainability. Wurd.

Comments

Popular Posts