Last week we looked at the first creational design pattern, the singleton. Most books start by examining all the creational patterns. But in this series, we’ll dip in and out, and look at interesting patterns without worrying about doing them all in order. This week we’ll look at the Adapter pattern.
Adapter Pattern as a Structural Pattern
The Adapter is a structural pattern. Structural patterns describe how classes and objects can be combined to form larger structures. These patterns work with the way that objects are combined with other objects. This ensures that any system changes don’t force these inter-connections to be changed.
The intent of the adapter pattern is to convert the interface of one class into that of another class that clients would prefer to use. Adapters let classes work together that wouldn’t otherwise because of incompatible interfaces.
We’re all familiar with adapters in real life. Mains electrical power is usually provided in houses via three-pin plug sockets. We often have electrical appliances with two-pin plugs which obviously don’t fit these sockets. How do we solve this problem? We use a three-pin to two-pin adapter. The same happens when we charge our cellphones. They need a 5 volt DC power input, but mains electricity is 220 volts AC. Again, we use an adapter.
Adapter Pattern Example
Let’s say we are modelling an irrigation controller system. The system needs to interface to valves, pumps, alarms, etc. These devices can be switched on and off, so we can model them as on/off devices. Firstly we’ll create an OnOffDevice
interface, as follows:
public interface OnOffDevice { public void on(); public void off(); }
Then we’ll code our concrete classes implementing this interface. Here is the Pump
; the others will be similar:
public class Pump implements OnOffDevice { public void on() { // appropriate code } public void off() { // appropriate code } }
Adapters
Looking carefully at the various OnOffDevice
s, we realise that although Pump
s and Alarm
s have on/off behaviours, a Valve
really has an open/close behaviour:
public class Valve { public void open() { // appropriate code } public void close() { // appropriate code } }
If we are purchasing valves from a valve supplier, the chances are very high that we won’t have access to their source code. So we won’t be able to modify it to implement our OnOffDevice
interface. However, we can easily adapt the open/close behaviour to an on/off behaviour by creating an Adapter class. This is an implementation of the standard Adapter design pattern.
public class OnOffValveAdapter implements OnOffDevice { private Valve valve; public OnOffValveAdapter(Valve valve) { this.valve = valve; } @Override public void on() { valve.open(); } @Override public void off() { valve.close(); } }
We use this class to wrap a normal Valve
to give it on/off behaviour, as follows:
// setting up Valve mainValve = new Valve(); OnOffValveAdapter mainValveAdapter = new OnOffValveAdapter(mainValve); // starting irrigation mainValveAdapter.on(); // more code... // stopping irrigation later mainValveAdapter.off();
As can be seen, this is a simple pattern to understand and implement.
What’s next?
In the weeks ahead, we’ll continue examining at some of the more useful design patterns. Stay tuned!
I’m always interested in your opinion, so please leave a comment. Your feedback helps me write tips that help you.