Policy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
Real-world example
Slaying dragons is a dangerous job. With experience, it becomes easier. Veteran dragonslayers have developed different fighting strategies against different types of dragons.
In plain words
Strategy pattern allows choosing the best-suited algorithm at runtime.
Wikipedia says
In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime.
Programmatic Example
Let’s first introduce the dragon-slaying strategy interface and its implementations.
1@FunctionalInterface
2public interface DragonSlayingStrategy {
3
4 void execute();
5}
6
7@Slf4j
8public class MeleeStrategy implements DragonSlayingStrategy {
9
10 @Override
11 public void execute() {
12 LOGGER.info("With your Excalibur you sever the dragon's head!");
13 }
14}
15
16@Slf4j
17public class ProjectileStrategy implements DragonSlayingStrategy {
18
19 @Override
20 public void execute() {
21 LOGGER.info("You shoot the dragon with the magical crossbow and it falls dead on the ground!");
22 }
23}
24
25@Slf4j
26public class SpellStrategy implements DragonSlayingStrategy {
27
28 @Override
29 public void execute() {
30 LOGGER.info("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!");
31 }
32}
And here is the mighty dragonslayer, who can pick his fighting strategy based on the opponent.
1public class DragonSlayer {
2
3 private DragonSlayingStrategy strategy;
4
5 public DragonSlayer(DragonSlayingStrategy strategy) {
6 this.strategy = strategy;
7 }
8
9 public void changeStrategy(DragonSlayingStrategy strategy) {
10 this.strategy = strategy;
11 }
12
13 public void goToBattle() {
14 strategy.execute();
15 }
16}
Finally, here’s the dragonslayer in action.
1 LOGGER.info("Green dragon spotted ahead!");
2 var dragonSlayer = new DragonSlayer(new MeleeStrategy());
3 dragonSlayer.goToBattle();
4 LOGGER.info("Red dragon emerges.");
5 dragonSlayer.changeStrategy(new ProjectileStrategy());
6 dragonSlayer.goToBattle();
7 LOGGER.info("Black dragon lands before you.");
8 dragonSlayer.changeStrategy(new SpellStrategy());
9 dragonSlayer.goToBattle();
Program output:
Green dragon spotted ahead!
With your Excalibur you sever the dragon's head!
Red dragon emerges.
You shoot the dragon with the magical crossbow and it falls dead on the ground!
Black dragon lands before you.
You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
What’s more, the lambda expressions in Java 8 provides another approach for the implementation:
1public class LambdaStrategy {
2
3 private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
4
5 public enum Strategy implements DragonSlayingStrategy {
6 MeleeStrategy(() -> LOGGER.info(
7 "With your Excalibur you severe the dragon's head!")),
8 ProjectileStrategy(() -> LOGGER.info(
9 "You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
10 SpellStrategy(() -> LOGGER.info(
11 "You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
12
13 private final DragonSlayingStrategy dragonSlayingStrategy;
14
15 Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
16 this.dragonSlayingStrategy = dragonSlayingStrategy;
17 }
18
19 @Override
20 public void execute() {
21 dragonSlayingStrategy.execute();
22 }
23 }
24}
And here’s the dragonslayer in action.
1 LOGGER.info("Green dragon spotted ahead!");
2 dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
3 dragonSlayer.goToBattle();
4 LOGGER.info("Red dragon emerges.");
5 dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
6 dragonSlayer.goToBattle();
7 LOGGER.info("Black dragon lands before you.");
8 dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
9 dragonSlayer.goToBattle();
The program output is the same as the above one.
Use the Strategy pattern when