Adapter Design Pattern in Java

Adapter pattern – one of the structural patterns – bridges the gap between two incompatible interfaces. This means that we can make classes work together that cannot otherwise because of incompatible interfaces. There are two approaches for implementing Adapter Pattern – class adapter and object adapter, however both approaches produce the same result. A class adapter uses multiple inheritance (by extending one class and/or implementing one or more interfaces) to adapt one interface to another. An object adapter uses Java composition by implementing one or more interfaces.

A real life example could be a case of mobile charger which acts as an adapter between wall socket and a mobile socket, because mobile battery needs 3 volts to charge but the normal socket produces much more than that.

Class Diagram

Adapter Pattern in Java

Implementation

We have Language class which represents a Language of speaking. We have LanguageProducer which produces only English language. And we have LanguageAdapter interface which has three methods for translating from English to different languages.

Language Class

Language producer class which produces only English Language

Language Adapter interface

Class Adapter Implementation

Here is the class adapter approach implementation for adapter pattern.

Object Adapter implementation

Adapter Pattern test class

Output

That’s all. Thank you for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.