[JAVA] Design pattern ~ Adapter ~

1.First of all

We will summarize the ** Adapter pattern ** in the GoF design pattern.

2. What is the Adapter pattern?

--The English word Adapter means ** what to adapt **. --The Adapter pattern is a method for converting ** already provided but not usable as it is into the required form **. -There are two methods, one is using ** inheritance ** and the other is using ** delegation **. -Sometimes called ** Wrapper pattern **. Wrapper means ** wrap **. --The GoF design patterns are classified as ** structural design patterns **.

3. Sample class diagram

3-1. ~ Method using inheritance ~

Adapter.PNG

3-2. ~ Method using delegation ~

Adapter2.PNG

4. Sample program

A program that displays student names and ages.

4-1. ~ Method using inheritance ~

4-1-1. Human class

This is the class originally provided.

Human.java


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);
	}
}

4-1-2. Student interface

The required interface.

Student.java


public interface Student {
	public abstract void showName();
	public abstract void showAge();
}

4-1-3. HumanAdapter class

This class serves as an adapter.

HumanAdapter.java


public class HumanAdapter extends Human implements Student {

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

	public void showName() {
		printName();
	}

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

4-1-4. Main class

This class performs the main processing.

Main.java


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

4-1-5. Execution result

Tanaka
25

4-2. ~ Method using delegation ~

4-2-1. Human class

This is the class originally provided.

Human.java


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);
    }
}

4-2-2. Student interface

The required interface.

Student.java


public interface Student {
    public abstract void showName();
    public abstract void showAge();
}

4-2-3. HumanAdapter class

This class serves as an adapter.

HumanAdapter.java


public class HumanAdapter implements Student {

	private Human human;

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

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

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

4-2-4. Main class

This class performs the main processing.

Main.java


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

4-2-5. Execution result

Tanaka
25

5. Benefits

The Adapter pattern covers an existing class to create the required class. This pattern allows you to quickly create the methods you need. Even if a bug is detected, if the existing class is well tested, you can focus on the class that plays the role of Adapter, which makes it easier to check the program. In addition, the Adapter pattern allows you to implement functionality without modifying existing classes, reducing the hassle of testing existing classes again.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

This article and sample program were created based on the following books.

-** Introduction to design patterns learned in Java language **

It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.

Recommended Posts

Design pattern ~ Adapter ~
Adapter Pattern
Design pattern ~ Builder ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Iterator pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Visitor pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
PrintObserver "Observer Design Pattern: Description and Implementation Example"
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -