October 2017
Intermediate to advanced
442 pages
12h 33m
English
The strategy design pattern is used to dynamically choose an implementation algorithm, a strategy, at runtime. The pattern is used, for example, to select different business algorithms depending on the circumstances.
We have several possibilities to make use of the strategy pattern, depending on the situation. We can define different implementations of an algorithm as separate classes. Java SE 8 includes the functionality of lambda methods and method references that can be used as a lightweight strategy implementation:
import java.util.function.Function;
public class Greeter {
private Function<String, String> strategy;
String greet(String name) {
return strategy.apply(name) + ", my name is Duke"; } public static void main(String[] ...Read now
Unlock full access