Fork me on GitHub
Tutorial Home

Adapter

Story

Allows that interface of an existing class to be used from another interface.

Adapters are often used in daily life, for example eletrical adapter is a device that converts attributes of one electrical device or system to those of an otherwise incompatible device or system. Some modify power or signal attributes, while others merely adapt the physical form of one electrical connector to another.

Image

alt text

Lionel Allorge, Adaptateur de prise française en prise suisse 2, CC BY-SA 3.0

UML

Class Adapter

Object Adapter

Implementation

Class Adapter

Target.java

package com.hundredwordsgof.adapter.clazz;

/**
 * 
 * Target interface, defines domain-specific interface to which Adaptee will be adapted 
 *
 */
public interface Target {

	String request();	
}

Adaptee.java

package com.hundredwordsgof.adapter.clazz;

/**
 * 
 * Adaptee class, interface which will be adapted 
 *
 */
public class Adaptee {

	public String specialRequest(){
		return "specialRequest";
	}
}

Adapter.java

package com.hundredwordsgof.adapter.clazz;

/**
 * 
 * Adapter class, adapts Adaptee to the Target interface
 *
 */
public class Adapter extends Adaptee implements Target {
	
	public String request() {
		return this.specialRequest();
	}
}

Object Adapter

Target.java

package com.hundredwordsgof.adapter.object;

/**
 * 
 * Target interface, defines domain-specific interface to which Adaptee will be adapted 
 *
 */
public interface Target {

	String request();	
}

Adaptee.java

package com.hundredwordsgof.adapter.object;

/**
 * 
 * Adaptee class, interface which will be adapted 
 *
 */
public class Adaptee {

	public String specialRequest(){
		return "specialRequest";
	}
}

Adapter.java

package com.hundredwordsgof.adapter.object;

/**
 * 
 * Adapter class, adapts Adaptee to the Target interface
 *
 */
public class Adapter implements Target {

  private Adaptee adaptee;

  public Adapter(Adaptee adaptee) {
    this.adaptee = adaptee;
  }

  public String request() {
    return adaptee.specialRequest();
  }
}

Usage

ClazzAdapterTest.java

package com.hundredwordsgof.adapter;

import static org.junit.Assert.*;
import org.junit.Test;
import com.hundredwordsgof.adapter.clazz.Adapter;
import com.hundredwordsgof.adapter.clazz.Target;

/**
 * Test implementation of the Adapter(object) pattern.
 */
public class ClazzAdapterTest {

  @Test
  public void testAdapter() {

    // creates Adapter
    Target target = new Adapter();

    assertEquals("specialRequest", target.request());
  }
}

ObjectAdapterTest.java

package com.hundredwordsgof.adapter;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.hundredwordsgof.adapter.object.Adaptee;
import com.hundredwordsgof.adapter.object.Adapter;
import com.hundredwordsgof.adapter.object.Target;

/**
 * Test implementation of the Adapter(object) pattern.
 */
public class ObjectAdapterTest {

  @Test
  public void testAdapter() {

    // creates Adaptee
    Adaptee adaptee = new Adaptee();

    // creates Adapter
    Target target = new Adapter(adaptee);

    assertEquals("specialRequest", target.request());
  }
}