Adapter Pattern in C#


Adapter Pattern

Download code from here

Introduction

GOF describes adapter as a pattern: “Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”

Motivation

We need to change our old system to new one.

When using third party API, we might need the wrapper which contains the functionality of the third party API. Yes, we can use the API directly on the client side but there may be the cases where the client doesn’t understand the object of third party API. In this scenario, we need the wrapper class which wrap-up the third party API to the class that client can understand. In real time scenario we might need the extra additional plug to connect to the electricity. That additional plug is an ‘Adaptor’.

Design

There are two ways we can implement Adapter, one using class and other using object. I will focus onto the later method in this article. Let’s see the class structure of both ways.

Adapter using class

Adapter using object

Adapter contains the instance of adaptee which needs to be change in type to Target.

Code

Full code can be downloaded from here.

I am using very simple example to demonstrate Adapter pattern using object.  Consider the ThirdPartyAPI class which is our adaptee and we needs to write wrapper for this so that we can use client familiar object i.e. FormualCalculator


/// <summary>

/// Adaptee class which needs to be converted into FormulaCalculator

/// </summary>

class ThirdPartyAPI

{

public void CalculateFormula()

{

Console.WriteLine("Third party tool formula calculation");

}

}

Adapter class


/// <summary>

/// Target class

/// </summary>

class FormulaCalculator

{

public virtual void CalculateFormula()

{

Console.WriteLine("CalculateFormula formula FormulaCalculator");

}

}

FormulaCalculatorAdaptor is used as wrapper for this purpose


/// <summary>

/// Adapter class

/// </summary>

class FormulaCalculatorAdapter : FormulaCalculator

{

private ThirdPartyAPI _api;

public FormulaCalculatorAdapter()

{

_api = new ThirdPartyAPI();

}

public override void CalculateFormula()

{

_api.CalculateFormula();

}

}

Conclusion

Adapter pattern is very useful when we need to write wrapper class on third party API.  Next consider the scenario that you have two different classes and you are using functionalities of both those classes in your client. The code would be cleaner and easy to understand if those both classes use same interface and we can use single interface in client. Adapter can play vital role in this scenario by providing common interface.

Advertisements

One thought on “Adapter Pattern in C#

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s