java · level 3 · unit 7 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is one swappable way of doing a job.
A is a well-known shape for a common problem. Naming it saves explaining it: say "strategy" and another developer knows the plan. You have met the idea already, because a Comparator is a strategy for sorting.
The strategy pattern puts each way of doing a job behind one interface. A shop might charge delivery at a flat rate, by weight, or not at all. Each rule is a lambda, and the checkout asks whichever rule it was given.
Keep strategies in a Map by name, and a new rule is one new line, with no edits to the checkout. Look them up with getOrDefault, so an unknown name gets a safe default instead of null.
import java.util.Map;
public class Main {
@FunctionalInterface
interface Delivery {
int costPence(int grams);
}
static String quote(Map<String, Delivery> rules, String method, int grams) {
Delivery rule = rules.getOrDefault(method, rules.get("standard"));
return method + ": " + rule.costPence(grams) + "p";
}
public static void main(String[] args) {
Map<String, Delivery> rules = Map.of(
"standard", grams -> 399,
"express", grams -> 500 + grams / 100 * 15,
"collect", grams -> 0);
System.out.println(quote(rules, "express", 2000));
System.out.println(quote(rules, "collect", 9500));
System.out.println(quote(rules, "pigeon", 300));
}
} quote never changes when a new rule arrives. That is the whole point of the pattern.
guess first
Nobody wrote a German greeting. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
import java.util.Map;
public class Main {
@FunctionalInterface
interface Greeting {
String greet(String name);
}
public static void main(String[] args) {
Map<String, Greeting> byLanguage = Map.of(
"en", name -> "Hello, " + name,
"fr", name -> "Bonjour, " + name,
"es", name -> "Hola, " + name);
Greeting fallback = byLanguage.get("en");
System.out.println(byLanguage.getOrDefault("fr", fallback).greet("Lea"));
System.out.println(byLanguage.getOrDefault("de", fallback).greet("Jonas"));
}
}
your turn
Your turn. Add a tipping strategy.
On line 11, add a generous tip strategy as a lambda, like a fifth of the bill.
output
press run to see what happens
fix it
An unknown voucher code crashes the till. Can you fix it?
output
press run to see what happens
- Line 14 throws
NullPointerException. What doescodes.gethand back for SUMMER? - SUMMER is not in the map, so
getgivesnull. A strategy is ready for that case already. - Change
codes.get(typed)tocodes.getOrDefault(typed, noDiscount)on line 13, then run.
nailed it
your code can swap strategies on the fly. :D
- A design pattern is a named, proven shape for a common problem.
- A strategy puts each way of doing a job behind one interface.
- Keep strategies in a map;
getOrDefaultgives a safe default.
new words in your collection
- design pattern
- strategy
This lesson isn’t saved. A free account remembers every lesson you finish.