Showing posts with label Adapter Pattern and my view about it. Show all posts
Showing posts with label Adapter Pattern and my view about it. Show all posts

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