Home > Design Patterns, dotnet 4, SOLID > Adapter Pattern

Adapter Pattern

In this post I will show you how you can use adapter pattern in your project. First of all the definition of Adapter pattern as wikipedia says is

  In computer programming, the adapter pattern (often referred to as the wrapper   pattern or simply a wrapper) is a design pattern that translates one interface for a   class into a compatible interface. An adapter allows classes to work together that   normally could not because of incompatible interfaces, by providing its interface to   clients while using the original interface.   

It’s some sort of start and I was a bit surprised that in Wikipedia article there was no example in C# ( I know most of the C# programmers were either sleeping or coding when the article was getting written 😉 ).

Anyway it doesn’t matter because a fantastic idea is a fantastic idea it doesn’t matter in which language it is written. It’s quite the same with food and music, if you like it you’ll love it , it doesn’t matter which culture or type it is.

As usual I will start with a simple but a real world example, this is something I have used it myself in a production project and it’s caching. Whether it’s a web or a thick client we all have used some sort of caching mechanism and we typically write something like this in ASP.NET



Well nothing wrong with this is how ASP.NET allows you to put something into the cache and you might be wondering what’s wrong with this. The problem I see with this code is that this code with be sprayed all across my project when ever I want to add something to cache, how do I test this ? Does it adds any value for the enterprise ?? (hmm sounding like an Architect )…well relax we haven’t committed a crime and lets see how we can improve it one step at a time..

So you might say lets start with an interface..good point, let’s do it.

public interface ICacheContext
    void Add(string cacheKey, object value);
    object Get(string cacheKey);

Ohh..and since we applied the YAGNI principle we almost forgot we need to add the remove method as well so that we can get rid of that item from the HttpCache. So the completed interface looks like ..

public interface ICacheContext
    void Add(string cacheKey, object value);
    object Get(string cacheKey);
    void Remove(string cacheKey);

Well looks good we can fake a context and easily test, also the class which will implement this interface will have the code and there will be no code smell. If in future Microsoft changes the Cache object to some other object it’s this class which has to change and I don’t have to do find and replace everywhere. So all is good so why am I still bothered ?

Well I don’t like the Context word in it,it looks more like web and as an architect when I am designing this interface I have to think at the enterprise level.

Would this interface will make sense if it is a thick client application or something else?? Isn’t caching is more about a temporary storage ?? And I am taking advantage of some of the cool things which .NET framework provides like instead of object I should use Generics ??

If we look into System.Web.Caching object the object browser you will see how I designed my ICacheContext interface.

System.Web.Cache in Object browser

The Ninja inside you awakes and puts some thoughts into it.

public interface ICacheStorage
    void Remove(string key);
    void Store(string key, object data);
    T Retrieve<T>(string key);

Ah now it makes sense it looks more real world interface, the interface is more of a conceptual thing with storage and the method names start to make more sense. Moreover this interface (contract) can be used for any kind of application and can be used enterprise wide ( for windows forms, windows service etc)

Now the Adapter pattern comes into act. It’s a very simple pattern and we all use it day in and day out but are not aware that we are using it.

As you saw ICacheContext we created before mimics the method System.Web.Cache object in the Framework. But the new contract we define has different set of methods ( like the different electric current and power point in different countries) and we need a Cache adapter which will adapt to our ICacheStorage interface and expose the Cache object functionality through these interface methods. ( like we have different power adapters for different countries)

Let see how do we describe the adapter pattern in UML class diagram

Adapter pattern UML class Diagram

Makes a lot of sense and the class diagram depicts exactly what we are intending to do.

So now we have to create this adapter which will implement the ICacheStorage interface and when implementing it’s methods it will map the ICacheStorage Remove method to the System.Web.Cache Remove method and so on.

And this is what I have come up with.

public class HttpContextCacheAdapter : ICacheStorage
    public void Remove(string key)

    public void Store(string key, object data)
        HttpContext.Current.Cache.Insert(key, data);
    public T Retrieve<T>(string key)
        T itemStored = (T)HttpContext.Current.Cache.Get(key) ?? default(T);
        return itemStored;

As you can see this class has a purpose, it implements our ICacheStorage and act as an adapter between the interface and the System.Web.Caching class. It also plays well with SOLID principle as you can see this class will only change if the underlying technology i.e HttpContext/Cache object changes.

By creating the ICacheStorage we have come up with a smart interface and be applied for different Caching strategy.For example you have windows service which uses a file system caching you can simply create a FileCacheStorage object and depending upon above scenario you could decide if an adapter is required or not.

This is my take no how to use the Adapter pattern and it’s a real world example, I hope it can help you refactor your code and encourage you to use it in your code.

  1. Yogo
    January 11, 2011 at 10:15 pm

    Hey Prashant,

    Once again a nice KISS (see http://en.wikipedia.org/wiki/KISS_principle) article. But you touched another topic here which is one of the extreme programming principles. YAGNI (see http://en.wikipedia.org/wiki/YAGNI). It would be great if you could elaborate a bit on these principles you reference in your articles. At least start off by referencing them to http://en.wikipedia.org to begin with.

    But truly nice work with these articles.

  1. December 9, 2010 at 1:36 am
    Poison Dart Frog Pictures |Cool Frog Diagram images - - Poison Dart Frog Pictures
  2. December 21, 2010 at 9:05 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: