The Adapter Pattern

The Adapter pattern can be used when you have a client that takes objects that conform to a certain interface but you also have another object that does not exactly conform to the interface that the client needs.

In other words (according to the Head First Design Patterns Book) it:

converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

In order to do this you create an Adapter class that implements the interface you would like it to convert the class to.

The example given in the book is that of a Duck interface:

public interface Duck {
String quack();
String fly();
}

Here is an example of a Duck:

public class MallardDuck implements Duck {

@Override
public String quack() {
return "Quack";
}

@Override
public String fly() {
return "I'm flying";
}
}

There’s also a Turkey interface:

public interface Turkey {
String gobble();
String fly();
}

Here is an example of a Turkey:

public class WildTurkey implements Turkey {
    @Override
public String gobble() {
return "Gobble gobble";
}

@Override
public String fly() {
return "I'm flying a short distance";
}
}

The Adapter

Now if a client wants to use the Turkey the same way it would use a Duck, it is necessary to create a turkey adapter class that implements a Duck interface.

public class TurkeyAdapter implements Duck {
Turkey turkey;

public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}

@Override
public String quack() {
return turkey.gobble();
}

@Override
public String fly() {
return turkey.fly();
}
}

So if you need to loop over a list of Ducks, it is now possible to use a Turkey Adapter in the mix and not error.

This is a somewhat contrived example and it is rare that you would want to force something to treat an object that implements a certain interface as one that implements another one. It might be possible that the code can be structured in a different way.

A useful real-world example is that of code that previously used the old Enumerator interface that Java had for some collections. It had the methods:

hasMoreElements()

nextElement()

With the introduction of the Iterator interface (hasNext(), next() and remove()) the Enumerator fell out of favour, but a lot of legacy code still has it. In order to tackle the problem of hiding the code with the old Interface it is possible to create an adapter that implements an Iterator interface out of the old Enumerator one so that you can use all the useful methods that the Iterator class has.

What problems does it solve?

The Adapter Pattern helps encapsulate the behaviour of a class hiding details that aren’t necessary to the client of that class, therefore making it easier to introduce polymorphism and reduce complexity in your code. I can’t see many uses for it, but I’m sure every once in a while it is useful to know it’s possible to use the Adapter Pattern.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.