[Java] Strategy pattern

Verification environment

Source code

StrategySample.java


package designpattern;

public class StrategySample {
	public static void main(String[] args) {
		say(new Dog());
		say(new Cat());
	}

	private static void say(Animal animal) {
		animal.say();
	}
}

Dog.java


package designpattern;

public class Dog implements Animal {

	@Override
	public void say() {
		System.out.println("one");
	}

}

Cat.java


package designpattern;

public class Cat implements Animal {

	@Override
	public void say() {
		System.out.println("Meow");
	}

}

Animal.java


package designpattern;

interface Animal {

	void say();
}

Impressions

This is what the Strategy pattern is, right? This is a code that often appears in the introduction of so-called polymorphism. This is because I have met many times at work without learning I could understand it easily.

It's the same as putting a HashMap in a Map

See you again (^_^) Noshi

Recommended Posts

[Java] Strategy pattern
Strategy pattern
Strategy Pattern
Enum Strategy pattern in Java
Java design pattern
java callback pattern
Design pattern ~ Strategy ~
[Java] Singleton pattern
[Java] Adapter pattern
Java pattern memo
My DAO pattern (Java)
Builder pattern (Effective Java)
Java design pattern summary
[Design pattern] Java core library
Java
[Java] Draw a simple pattern
Java
[Java] Change the process according to the situation with the Strategy pattern
Think of a Java update strategy
JNA (Java Native Access) pattern collection
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
Memento Pattern
[Java] Annotation
[Java] Module
Java array
What a Strategy pattern Factory, not a State
Mediator pattern
Iterator pattern
Studying Java ―― 9
Java scratch scratch
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
Java string
Bridge Pattern
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Command Pattern
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
Builder Pattern