Design Pattern | Adapter Pattern

What Is the Adapter Pattern?

  • The English word adapter means something that adapts.
  • The Adapter pattern is a method for converting something already provided into the required form when it cannot be used as-is. In other words, it is a way to change an interface.
  • Think of an adapter plug that connects in the middle when the voltage does not match.
  • In the pattern, if there are two incompatible interfaces on both sides, you create a bridge that connects them.
  • There are methods using inheritance and methods using delegation.
    • Adapter Pattern by class
      • An Adapter Pattern that uses inheritance.
    • Adapter Pattern by instance
      • An Adapter Pattern that uses delegation.
  • It is also called the Wrapper pattern. Wrapper means something that wraps.
  • In GoF design patterns, it is classified as a structural design pattern.

Adapter Pattern Example Program

This example program displays a student’s name and age.

1. Method Using Class Inheritance

Class Diagram
Adapter Pattern Class Diagram

1-1. Human Class

This is the originally provided class.

Human.java

package com.devkuma.designpattern.structural.adapter.ex1;

public class Human {

    private String name;
    private int age;

    public Human(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void printName() {
        System.out.println(name);
    }

    public void printAge() {
        System.out.println(age);
    }
}

1-2. Student Interface

This is the required interface.

Student.java

package com.devkuma.designpattern.structural.adapter.ex1;

public interface Student {
    void showName();

    void showAge();
}

1-3. HumanAdapter Class

This class plays the Adapter role.

HumanAdapter.java

package com.devkuma.designpattern.structural.adapter.ex1;

public class HumanAdapter extends Human implements Student {

    public HumanAdapter(String name, int age) {
        super(name, age);
    }

    public void showName() {
        printName();
    }

    public void showAge() {
        printAge();
    }
}

1-4. Main Class

This is the class that executes the main processing.

Main.java

package com.devkuma.designpattern.structural.adapter.ex1;

public class Main {
    public static void main(String[] args) {
        Student student = new HumanAdapter("devkuma", 25);
        student.showName();
        student.showAge();
    }
}

1-5. Execution Result

devkuma
25

2. Method Using Delegation

Adapter Pattern Class Diagram

2-1. Human Class

This is the originally provided class.

Human.java

package com.devkuma.designpattern.structural.adapter.ex2;

public class Human {

    private String name;
    private int age;

    public Human(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void printName() {
        System.out.println(name);
    }

    public void printAge() {
        System.out.println(age);
    }
}

2-2. Student Interface

This is the required interface.

Student.java

package com.devkuma.designpattern.structural.adapter.ex2;

public interface Student {
    void showName();

    void showAge();
}

2-3. HumanAdapter Class

This class plays the Adapter role.

HumanAdapter.java

package com.devkuma.designpattern.structural.adapter.ex2;

public class HumanAdapter implements Student {

    private Human human;

    public HumanAdapter(String name, int age) {
        this.human = new Human("田中", 25);;
    }

    public void showName() {
        human.printName();
    }

    public void showAge() {
        human.printAge();
    }
}

2-4. Main Class

This is the class that executes the main processing.

Main.java

package com.devkuma.designpattern.structural.adapter.ex2;

public class Main {
    public static void main(String[] args) {
        Student student = new HumanAdapter("devkuma", 25);
        student.showName();
        student.showAge();
    }
}

2-5. Execution Result

devkuma
25

Advantages of the Adapter Pattern

The Adapter pattern wraps an existing class and creates the class that is needed. Using this pattern, you can quickly create a required group of methods.
Even if a bug is detected, if the existing class has been tested sufficiently, you can focus on investigating the Adapter class, making program checking easier.
Also, because the Adapter pattern can implement functionality without modifying the existing class, it reduces the effort of retesting the existing class.