Friday 16 March 2007

Adapter Pattern

In this week’s lecture we covered the Adapter Pattern. In this blog I give an overview of the Adapter Pattern and my opinion about it.

An Adapter Pattern is one of the structural patterns by GOF. Its used in situation where you have one class that you wish to reuse, but the application interface does not match the class interface.[1] The Adapter is therefore used to convert the application interface to the existing class interface. Put simply, the Adapter maps the interface of one class to that of another.

I especially like the example of an electric socket that was used in class to explain the adapter pattern.

How the Adapter pattern works

A Client wants to invoke the method request() in the Target interface. Since the Adaptee class has no request() method, it is the job of Adapter to convert the request to an available matching method. Here the Adapter converts the method request() call into the Adaptee method specificRequest() call. The Adapter does this conversion for each method that needs adapting.

The two types of Adapter Patterns are:

1. Object Adapter Pattern(contains an instance of the class it wraps)
2. Class Adapter Pattern (using multiple inheritance)

From the class discussion, I think this is one of the most important patterns in software development.

With the Adapter Pattern, one does not have to change local objects, the adapter takes care of the job.

Beside, the adapter pattern reduces the creation of excessively heavy and low performance remote objects. Therefore lighter programmes can be developed.

Since adapter separated responsibilities along class lines, one produces a design that is easy to debug and code the in easier to read.

Like other patterns, once created, adapters can be used over and over again saving both effort and time.


Though adapter pattern provides the benefits highlighted above, I have noted a few issues with them.

I think one must be carefully when using the Adapter pattern. When the design of the Adapter is poor, even if the applications/interfaces that the adapter intends to adapt are good, the design of the adapter may negatively affect the overall performance of the classes involved.

The scope of adapting that an adapter does also needs to be considered. Rather than improving performance between the adapted class the adapter may compromise functionality of one of the classes hence affecting performance of both classes whenever they interface.


References

[1]
Dan Becker,
Design Networked Applications in RMI Using the Adapter Pattern,
A guide to correctly adapting local Java objects for the web
(5/01/99)
http://www.javaworld.com/javaworld/jw-05-1999/jw 05networked.html?page=1
Accessed: 16th March 2007

[2]
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Design Patterns,
Elements of Reusable Object-Oriented Software,
(1995)
Addison-Wesley
Page 139 – 150

[3]

James W Copper,
Java Design Patterns,
(2000)
Addison Wesley
Page 71 - 79

6 comments:

Abbas said...

from what ever i understood about this pattern, i also agree with you that it is one of the most important pattern in OOP, because it creates a platform for application/programs that are not compatible with eachother, so that they can communicate with eachother.

John Ssebaale said...

Hi I have been readin on the Decorator Pattern, again this seems to be the most commonly used. Well I looked at the statistics on its usage, the figure were astonishing. I think we can make the final judgement when we look at all the 23 patterns. Thanks for the comment.
Cheers

seps said...

nice compilation however a bit technical for a lay man like me. the terms used like invoke, could be substituted with lighter terms

Daljit @ Dil said...

Good flow.I would like to share my views and I think every patterns has its own uniqueness and therefore they play their roles accroding to their strengths.
As you raised that there are issues opposite the benefits there is one which I like to highlight, which is Multiple Inheritance.As a solution it multiply inherits the adapter class from the target class to include both the implementations but this technique will not apply to languages that do not support multiple inheritance. Languanges such as C# and VB.net do not support multiple inheritance but only single inheritance.

Goodday.

richie rich said...

Hi John good explanation i gathered a lot of information on the pattern from your blog. i dont think its the most crital pattern of the 23 design patterns but it plays and important role in software development. Unless you are an expertor have used all 23 then that statement is not valied.

Another benefit of adapter pattern is that its you can use it over different programming language platforms, but well done and said.

Gayan lakshitha said...

I agreed with your clear explanation.you explained with clear example.I think you have solved my problem in course work.