Your Guide to Design Patterns – Adapter Pattern

Design Patterns - Adapter Pattern

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 OnOffDevices, we realise that although Pumps and Alarms 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Code like a Java Guru!

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email info@incusdata.com

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.

Your Java tip is on its way!

Check that incusdata.com is an approved sender, so that your Java tips don’t land up in the spam folder.

Our privacy policy means your data is safe. You can unsubscribe from these tips at any time.