Compilation of already published Articles/Ideas/Problems-Solutions which I faced or came across over the period of time. Largely a place for me to document it as note-to-self. Nothing serious. :)

Sunday, October 6, 2013

Adapter Design Pattern

07/05/2011

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need.
In real world the easy and simple example that comes to mind for an adapter is the travel power adapter. American socket and plug are different from British. Theirinterface are not compatible with one another. British plugs are cylindrical and American plugs are recangularish. You can use an adapter in between to fit an American (rectangular) plug in British (cylindrical) socket assuming voltage requirements are met with.

How to implement adapter design pattern?

Adapter design pattern can be implemented in two ways. One using the inheritance method and second using the composition method. Just the implementation methodology is different but the purpose and solution is same.

Adapter implementation using inheritance

When a class with incompatible method needs to be used with another class you can use inheritance to create an adapter class. The adapter class which is inherited will have new compatible methods. Using those new methods from the adapter the core function of the base class will be accessed. This is called “is-a” relationship. The same real world example is implemented using java as below. Dont worry too much about logic, following example source code attempts to explain adapter design pattern and the goal is simplicity.

publicclassCylindricalSocket {

publicString supply(String cylinStem1, String cylinStem1) {

System.out.println("Power power power...");

}

}

publicclassRectangularAdapter extendsCylindricalSocket {

publicString adapt(String rectaStem1, Sting rectaStem2) {

//some conversion logic

String cylinStem1 = rectaStem1;

String cylinStem2 = rectaStem2;

returnsupply(cylinStem1, cylinStem2);

}

}

publicclassRectangularPlug {

privateString rectaStem1;

privateString rectaStem2;

publicgetPower() {

RectangulrAdapter adapter = newRectangulrAdapter();

String power = adapter.adapt(rectaStem1, rectaStem2);

System.out.println(power);

}

}

Adapter implementation using composition

The above implementation can also be done using composition. Instead of inheriting the base class create adapter by having the base class as attribute inside the adapter. You can access all the methods by having it as an attribute. This is nothing but “has-a” relationship. Following example illustrates this approach. Difference is only in the adapter class and other two classes are same. In most scenarios, prefer composition over inheritance. Using composition you can change the behaviour of class easily if needed. It enables the usage of tools like dependency injection.