Java Coding Nerd. This Blog will feature content-rich articles, tutorials and own opinions on variuos programming related topcis.

Design Patterns, Java Tutorial

Java Design Patterns: Adapter Pattern



The Adapter Pattern in Theory

In software engineering, the Adapter Pattern is a Pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without knowing or altering their source code.

It concerns following problems:

  • How can a class be reused that does not have an interface that a client requires?
  • How can classes that have incompatible interfaces work together?
  • How can an alternative interface be provided for a class?


  • It adds another layer of complexity
  • as the Interface changes the adapter has to be refactored too
  • adds boilerplate code that does not add business value


Let´s get practical

So lets assume we have a Interface called IndianKitchen and a Chief that implements this Interface.

public interface IndianKitchen {

  void cook();



public class Chief implements IndianKitchen {

  private IndianKitchen indianFood;

  public Chief() {}

  public Chief(IndianKitchen indianFood) {
    this.indianFood = indianFood;

  public void cook() {

Our customers come to find that indian food is pretty exhaustive for their stomach if they eat it all the time. We would like the Chief to also cook some tasty italian food.  But at the same time we are not allowed to touch the code of the Chief class.
This is where the Adapter Patter comes into play. First we define our ItalianKtichen

public class ItalianKitchen {

  private static final Logger LOGGER = LoggerFactory.getLogger(ItalianKitchen.class);

  public void cook() {"Mhh some Pizza and Pasta");


What now? Chief is still not able too cook italian food.  Lets wright the adapter

public class ItalianKitchenAdapter implements IndianKitchen {

  private ItalianKitchen italianFood;

  public ItalianKitchenAdapter() {
    italianFood = new ItalianKitchen();

  public void cook() {

Our adapter implements the IndianKitchen Interface, so because of polymorphism the Chiefs can work with this adapter. By implementing the IndianKitchens method with the ItalianKitchen functionality we now have a chief that is able to cook some nice pasta (even though he thinks he is cooking indian 😛 )

You can test it by simply running the code

public static void main(String[] args) {
   Chief chief= new Chief(new ItalianKitchenAdapter());



Spread the love

Leave a Reply

Theme by Anders Norén