Tuesday, 3 April 2007

ADAPTER PATTERN

In our pursuit to understand and appreciate patterns more, we recently discussed the adapter pattern.
An adapter, one of those catalogued by the Gang of Four, allows classes to work together despite their incompatible interfaces by wrapping its own interface around that of an already existing class. The adapter is also responsible for handling any logic necessary to transform data into a form that is useful for the consumer. We discussed an example of where an electric adapter in the UK is different from the one in the USA. In order to use an appliance in both countries one needs a converter for the differential power usage by the electric appliance. This converter is the mechanism to overcome the incompatibility in power usage and socket usage hence the adapter.

There are two types of adapter patterns:

The Object Adapter pattern This implements a desired interface which clients require to hold to hold the instance of the adaptee. The adapter inherits the target interface that the client expects to use, and relates object in question to it so as to be used by the adopting target. When the client calls the request () method on its target object (the adapter), the request is translated into the corresponding specific request on the adaptee.
The object adapter is advantageous in a way that since it does not use inheritance of the adptee, it can wrap the adptee and any derivatives of the adpatee. Secondly, unlike the class adapter where all methods of the target are exposed to the client the object adapter may choose the desired methods that it wants to expose to the client and there is no restriction of multiple inheritance requirements.
It is disadvantageous in a way that it makes harder to override the target behavior because it will require inheriting from the adaptee and making the wrapper refer to the subclass rather than the adpatee itself.

Applicability
This is generally used when the client and the adptee need to be completely decoupled from each other where one will be overridden by the other’s interface.


The Class Adapter pattern
This type of adapter uses multiple inheritances to achieve its goal. The adapter is created inheriting interfaces from both the interface that is expected and the interface that is pre-existing. The Object Adapter pattern is more often used in some popular languages, such as Java; do not support true multiple inheritances as the designers of these languages consider it a dangerous practice. Class adapters use multiple inheritances to achieve their goals. As in the object adapter, the class adapter inherits the interface of the client's target. However, it also inherits the interface of the adaptee as well. Since Java does not support true multiple inheritance, this means that one of the interfaces must be inherited from a Java Interface type. The request to the target is simply rerouted to the specific request that was inherited from the adaptee interface.

Class adapters have a problem with name conflicts if methods of the same signature exist on both the target and the adaptee. Note that just because two objects have methods that have the same signature (syntax), it does not guarantee that the two methods have the same meaning or behavior (sematics). That is, the two methods do not necessarily map directly to each other. Object adapters do not have this problem.
Class adapters are simpler than object adapters in that they involve fewer classes and are useful if total decoupling of the client and adaptee is not needed.



Advantageous in that it changes of some adapted class methods will still allow the other methods to be unchanged and Lets you override the common methods from the interface class in the wrapper class to call the methods in the adaptee class as appropriate. This design results in an efficient run time performance.

Disadvantageous due to the fact that Only the adaptee class can be adapted but its other subclasses cannot be used and is applicable only for languages that support multiple inheritances in some cases. It requires multiple inheritances.

Applicability
It is generally used in applications in which there is a need to use an existing class but the interface of the target class does not match. It has common methods from the target interface class and the adaptee class.

http://www.designpattern.lu.unisi.ch/
http://ww1.ucmss.com/books/LFS/CSREA2006/SER4955.pdf

1 comment:

Basco said...

Hi Steve,

Good job done, but i want to highlight a bit on your real world example of Adaptor patterns. I can understand what you are trying imply because i was in the class but i think a novice would not. i think your example should have been more clearer. If am right about your real world example, it should be in order to use UK electrical appliance in the US or vice versa you need a converter this will serve as an interface between the appliance and the socket. But not in order to use an appliance in both countries one needs a converter......