Adapter

Introduction

When you have two or more interfaces, one of which is incompatible for your needs, you need to modify it. But usually it’s prohibitive to modify the original code, a better way is to use a wrapper to translate the interface to the compatible one. The Adapter pattern will meet your need. For instance, if you have a interface DateUtil#getDate which returns the current date as the “YYYYMMDD”, but actually you need another format like “MM/DD/YYYY”, and it’s impossible or too expensive to modify the DateUtil class. You may write a new class as a wrapper, which we call it adapter to translate it.

Definition

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

UML class diagram

Class Adapter

A class adapter uses multiple inheritance to adapt one interface to another: class-adapter

Object Adapter

An object adapter relies on object composition object-adapter

Participants

  • Target
    • defines the domain-specific interfaces that Client uses.
  • Client
    • collaborates with objects conforming to the Target interface.
  • Adaptee
    • defines an existing interface that needs adapting.
  • Adapter
    • Adapts the interface of Aaptee to the Target interface.

Example

// Target
public interface Targetable {
	public void request();
}

// Adaptee
public class Adaptee {
	public void specficRequest() {
		System.out.println("This is Adaptee#specficRequest.");
	}
}

// ClassAdapter
public class ClassAdapter extends Adaptee implements Targetable {

	@Override
	public void request() {
		// TODO Auto-generated method stub
		System.out.println("This is class adapter.");
		specficRequest();
	}

}

// ObjectAdapter
public class ObjectAdapter implements Targetable {

	private Adaptee adaptee = new Adaptee();
	
	@Override
	public void request() {
		// TODO Auto-generated method stub
		System.out.println("This is object adapter.");
		adaptee.specficRequest();
	}

}

// Client
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Targetable target = new ClassAdapter();
		target.request();
		
		System.out.println("------------");
		target = new ObjectAdapter();
		target.request();
	}

}

Output:

This is class adapter.
This is Adaptee#specficRequest.
------------
This is object adapter.
This is Adaptee#specficRequest.

Usage

  • When you want to use an existing class, and its interface does not match the interface you need.
  • When you want to create a reusable class that coopreates with unrelated onforeseen classes, that is, classes that don’t necessarily have compatible interfaces.
  • (object adapter only) When you need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

Summary

Adapter pattern is a common pattern we use in software design, especially when you’re using a third-party plugin. And as a matter of fact, Object Adapter is recommended rather than Class Adapter.

See Also

blog comments powered by Disqus