by Lars Vogel

Follow me on twitter

Lars Vogel on Google+

Adapter Design Pattern in Java

Lars Vogel

Version 0.1

01.07.2009

Revision History
Revision 0.1 01.07.2009 Lars
Vogel
Separated from http://www.vogella.de/articles/DesignPatterns/article.html

Adapter

This article describes the Design Pattern "Adapter" and its usage in the programming language Java.


Table of Contents

1. Adapter
1.1. Definition
1.2. Example
1.3. Evaluation
2. Thank you
3. Questions and Discussion
4. Links and Literature

1. Adapter

1.1. Definition

The Adapter Pattern converts the interface of a class into another interface the clients expects. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

1.2. Example

We have a database access class which provides the access to database tables and returns the data. This data should be displayed in a tree (either swing or swt). The table expects the data to be provided via a certain interfaces. Instead of adding this interface to the database access class (which would couples the GUI coding very tight to the database access class), we develop an adapter which converts the interfaces of the database access class to the interfaces of the tree.

1.3. Evaluation

By implementing an interface we can replace the adapter and or the underlying model easily which another adapter / model. Hence the application is loosely coupled.

Adapter allows to re-use existing coding without changing it. The adapter will make the conversion between the interfaces.

In comparison to a decorator the adapter pattern only converts interfaces, while the decorator pattern adds new functionality to an existing interface. Therefore the decorator does not change the existing interface.

2. Thank you

Thank you for practicing with this tutorial.

3. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.de Google Group. I have created a short list how to create good questions which might also help you.

4. Links and Literature

http://en.wikipedia.org/wiki/Design_Patterns Wikipedia Entry about the GOF Design Pattern book.