[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
Adapter Pattern
Java design pattern
java callback pattern
[Java] Singleton pattern
Design pattern ~ Adapter ~
Java pattern memo
My DAO pattern (Java)
Builder pattern (Effective Java)
Java Lambda Command Pattern
Java design pattern summary
[Design pattern] Java core library
Java
Enum Strategy pattern in Java
[Java] Draw a simple pattern
Java
JNA (Java Native Access) pattern collection
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Java learning (0)
Studying Java ―― 3
[Java] array
Memento Pattern
[Java] Annotation
[Java] Module
Java array
Mediator pattern
Iterator pattern
Java tips, tips
Composite pattern
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
Observer Pattern
java (override)
java (method)
Builder pattern
Java Day 2018
Bridge Pattern
java (array)
Java static
Java serialization
JAVA paid
Command Pattern
Java (set)
java shellsort
Studying Java -5
Builder Pattern
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Iterator Pattern