Home Tutorials Training Consulting Products Books Company Donate Contact us









Online training

Events

Quick links

Share

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

1. Overview of the adapter pattern

1.1. Description

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

The adapter pattern describes how to convert an object into another object which a clients expects. This pattern mainly adapts one object to another one. Adapters allow objects to work together that couldn’t otherwise because of incompatible interfaces.

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 objects, while the decorator pattern adds new functionality to an existing object. Therefore, the decorator does not change the existing interface.

1.2. Example for an adapter in Java

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.

1.2.1. 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.

1.2.2. 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.

2. Links and Literature

3. vogella training and consulting support

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