We will summarize the ** Adapter pattern ** in the GoF design 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 **.
A program that displays student names and ages.
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);
}
}
The required interface.
Student.java
public interface Student {
public abstract void showName();
public abstract void showAge();
}
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();
}
}
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();
}
}
Tanaka
25
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);
}
}
The required interface.
Student.java
public interface Student {
public abstract void showName();
public abstract void showAge();
}
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();
}
}
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();
}
}
Tanaka
25
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.
-** GoF design pattern summary **
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