Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

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

1. The adapter pattern

The adapter pattern converts the interface of a class into another interface the clients expects. This pattern mainly adapts one object to another one. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

The adapter pattern is widely known in software development and used in many programming languages, e.g., Java.

Adapter allows to reuse existing coding without changing it, as the adapter ensures the conversion between the different interfaces.

In comparison to a decorator pattern, 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. Example for an adapter

2.1. Target

Let’s implement a real world adapter like a power adapter. Different countries sometimes have different electrical sockets. In order to make different electrical sockets work together with a different plugs the use of adapters is necessary.

2.2. The model for getting electricity

public class GermanElectricalSocket {

    public void plugIn(GermanPlugConnector plug) {
        plug.giveElectricity();
    }
}
public interface GermanPlugConnector {

    public void giveElectricity();
}
public class UKElectricalSocket {

    public void plugIn(UKPlugConnector plug) {
        plug.provideElectricity();
    }
}
public interface UKPlugConnector {

    public void provideElectricity();
}

These classes make clear that only UKPlugConnectors can be plugged into a UKElectricalSocket and only GermanPlugConnectors can be plugged into a GermanElectricalSocket.

Fortunately an UKElectricalSocket can also be used with a GermanPlugConnector by using an adapter. This can be archived by wrapping a GermanPlugConnector in a UKPlugConnector.

2.3. Creating an adapter for plug connectors

To use the plugIn of UKElectricalSocket an `UKPlugConnector has to be used. Therefore, the GermanPlugConnector is wrapped in a new class, which implements the UKPlugConnector interface.

public class GermanToUKPlugConnectorAdapter implements UKPlugConnector {

    private GermanPlugConnector plug;

    public GermanToUKAdapter(GermanPlugConnector plug) {
        this.plug = plug;
    }

    @Override
    public void provideElectricity() {
        plug.giveElectricity();
    }

}

The adapter can then be used like this:

GermanPlugConnector plugConnector = //.. create a GermanPlugConnector
UKElectricalSocket electricalSocket = new UKElectricalSocket();
UKPlugConnector ukAdapter = new GermanToUKAdapter(plugConnector);
electricalSocket.plugIn(ukAdapter);

With this adapter in between the GermanPlugConnector can now also make use of the UKElectricalSocket.

3. About this website

4. Links and Literature

4.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.