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

package com.roytuts.designpattern.adapter;

public class Language {

  private String lang;

  public Language(String lang) {
    this.lang = lang;
  }

  public String getLang() {
    return lang;
  }

  public void setLang(String lang) {
    this.lang = lang;
  }

}

Language producer class which produces only English Language

package com.roytuts.designpattern.adapter;

public class LanguageProducer {

  public Language getEnglishLang() {
    return new Language("English");
  }

}

Language Adapter interface

package com.roytuts.designpattern.adapter;

public interface LanguageAdapter {

  Language translateToBengali();

  Language translateToGermany();

  Language translateToFrench();

}

Class Adapter Implementation

Here is the class adapter approach implementation for adapter pattern.

package com.roytuts.designpattern.adapter;

public class LanguageClassAdapterImpl extends LanguageProducer implements LanguageAdapter {

  @Override
  public Language translateToBengali() {
    return new Language("Translation from " + getEnglishLang().getLang() + " to Bengali");
  }

  @Override
  public Language translateToGermany() {
    return new Language("Translation from " + getEnglishLang().getLang() + " to Germany");
  }

  @Override
  public Language translateToFrench() {
    return new Language("Translation from " + getEnglishLang().getLang() + " to French");
  }

}

Object Adapter implementation

package com.roytuts.designpattern.adapter;

public class LanguageObjectAdapterImpl implements LanguageAdapter {

  private LanguageProducer lp = new LanguageProducer();

  @Override
  public Language translateToBengali() {
    return new Language("Translation from " + lp.getEnglishLang().getLang() + " to Bengali");
  }

  @Override
  public Language translateToGermany() {
    return new Language("Translation from " + lp.getEnglishLang().getLang() + " to Germany");
  }

  @Override
  public Language translateToFrench() {
    return new Language("Translation from " + lp.getEnglishLang().getLang() + " to French");
  }

}

Adapter Pattern test class

package com.roytuts.designpattern.adapter;

public class AdapterPatternTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    testClassAdapter();
    System.out.println();
    testObjectAdapter();
  }

  private static void testClassAdapter() {
    LanguageAdapter adapter = new LanguageClassAdapterImpl();
    System.out.println(adapter.translateToBengali().getLang());
    System.out.println(adapter.translateToGermany().getLang());
    System.out.println(adapter.translateToFrench().getLang());
  }

  private static void testObjectAdapter() {
    LanguageAdapter adapter = new LanguageObjectAdapterImpl();
    System.out.println(adapter.translateToBengali().getLang());
    System.out.println(adapter.translateToGermany().getLang());
    System.out.println(adapter.translateToFrench().getLang());
  }
}

Output

Translation from English to Bengali
Translation from English to Germany
Translation from English to French

Translation from English to Bengali
Translation from English to Germany
Translation from English to French

That’s all. Thank you for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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