This blog is about patterns in real life, design pattern, software patterns, the Gang of Four patterns and anti-pattern relating to Software Development.

Friday, 27 April 2007

Adapter Pattern

[Blog 9]

Adapter Pattern is used to convert the interface of one class to another so that it meets the requirements of the client. Adapter pattern solves the problem of incompatible interfaces of classes that need to work together. Usually, the interfaces are fixed without modifying the objects and classes directly. The adapter pattern is also known as “Wrapper”. [1]

The wrapper object is known as the Adapter and the object it wraps is referred to as the Adaptee. [2]

In some situations, reusable classes cannot be really be reused because of unpredictable situations like change in program structure after a period of time that makes the class incompatible with new classes.

A real life example is of a European type plug being fitted to an American type power socket which will not work obviously because they have different connections/interfaces which make them incompatible. In order for them to work with each other, a converter needs to be fitted, that converts the plug to adapt to the socket.

There are two kinds of adapters, namely, class adapter and object adapter. A class adapter changes the adaptee in such a way so that it conforms to the interface required but unfortunately, this does not work when the subclasses have to be adapted as well as they have their own characteristics. Since the adapted is derived from the adaptee, the adapted can override its activities.

Object adapters are simpler, as they allow the adapter to work with both the object and the sub-classed objects. So functions can be added to the object class and the sub classes as well. Unfortunately, because sub class objects have their own characteristics, overriding the adaptee’s behaviour is difficult as it would require each sub class object to be considered first. Finally, adapters have the ability to have some of their own functions, which gives the ability of adding functionality. The disadvantage of adapters is that extra lines of codes have to be added and maintained.

It should be noted that object adapters use object composition whereas class adapters use inheritance. [3]

References:
[ref.1] - Design Patterns for Dummies, by Steve Holzner, Wiley Publishing Inc., 2006, ISBN: 978-0-471-79854-5 pp.121

[ref.2] - Software Architecture Design Patterns in Java, by Partha Kuchana 2004, ISBN 0-8493-2142-5, ch.20, p.213.

[ref.3] - Design Patterns for Dummies, by Steve Holzner, Wiley Publishing Inc., 2006, ISBN: 978-0-471-79854-5 pp.134

2 Comments:

Blogger Pauls Matuta said...

I like your approach to adaptor with a clear real life example. I would have loved to see other like distributed object databases such as CORBA which use Adaptors to integrate native language objects into the database.

Adaptors are also used to make non-objects, like text files, look like objects.

I hope this is meaningful

14 May 2007 at 21:37

 
Blogger SunnyPAFSD said...

Thanks for the comment.

I am not too sure of non-objects adapted to look like objects. But i take it onboard.

thanks Pauls

14 May 2007 at 22:23

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home