[Java] Adapter pattern

Verification environment

Source code for verification

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

Class diagram

Adapterクラス図.png

Impressions

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

[Java] Adapter pattern
Design pattern ~ Adapter ~
Java pattern memo
Builder pattern (Effective Java)
Java design pattern summary
Java
Java beginner design pattern (Factory Method pattern)
[Java] Object-oriented
Java iteration
ML. Java
Studying Java ―― 2
Facade pattern
Studying Java ①
[Java] System.out.println ()
java (polymorphism)
java rock-paper-scissors
Java basics
java array
[Java] Array
java (inheritance)
java installation
Studying Java -10
Bridge pattern
Java Tips
[Java] logback-spring.xml
Java (clear)
[Java] method
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -