For example, let's say you have the following Oimo class. You can taste this class.
Oimo.java
package adapter;
public class Oimo {
public String feelTaste() {
return "Delicious";
}
}
However, I want information about potatoes when I eat them. Declare as follows. The NewAbility class and NewOimo class that appear here are the protagonists of the Adapter pattern.
User.java
package adapter;
public class User {
public static void main(String[] args) {
NewAbility oimo = new NewOimo("Beni Azuma");
System.out.println(oimo.oimoInfo());
}
}
First, let's create a NewOimo class that inherits the Oimo class. By the way, let's give new ability so that we can get potato information.
NewOimo.java
package adapter;
public class NewOimo extends Oimo implements NewAbility {
public final String name;
public NewOimo(String name) {
this.name = name;
}
@Override
public String oimoInfo() {
return getName() + feelTaste();
}
private String getName() {
return this.name;
}
}
Next, let's create a NewAbility interface that bridges the User class and the NewOimo class and delegate it to the NewOimo class.
NewAbility.java
package adapter;
public interface NewAbility {
public String oimoInfo();
}
This Adapter pattern seems to be useful when you want to use existing code but cannot use it directly because the IF is different. Let's remember! Isn't it used for new development?
See you again (^_^) Noshi
Recommended Posts