Skip to content

Commit d2724e8

Browse files
committed
iluwatar#590 add explanation for Strategy
1 parent 9a81ddb commit d2724e8

File tree

1 file changed

+100
-3
lines changed

1 file changed

+100
-3
lines changed

strategy/README.md

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,106 @@ tags:
1212
Policy
1313

1414
## Intent
15-
Define a family of algorithms, encapsulate each one, and make them
16-
interchangeable. Strategy lets the algorithm vary independently from clients
17-
that use it.
15+
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary
16+
independently from clients that use it.
17+
18+
## Explanation
19+
20+
Real world example
21+
22+
> Slaying dragons is a dangerous profession. With experience it becomes easier. Veteran dragonslayers have developed different fighting strategies against different types of dragons.
23+
24+
In plain words
25+
26+
> Strategy pattern allows choosing the best suited algorithm at runtime.
27+
28+
Wikipedia says
29+
30+
> 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.
31+
32+
**Programmatic Example**
33+
34+
Let's first introduce the dragon slaying strategy interface and its implementations.
35+
36+
```java
37+
@FunctionalInterface
38+
public interface DragonSlayingStrategy {
39+
40+
void execute();
41+
}
42+
43+
public class MeleeStrategy implements DragonSlayingStrategy {
44+
45+
private static final Logger LOGGER = LoggerFactory.getLogger(MeleeStrategy.class);
46+
47+
@Override
48+
public void execute() {
49+
LOGGER.info("With your Excalibur you sever the dragon's head!");
50+
}
51+
}
52+
53+
public class ProjectileStrategy implements DragonSlayingStrategy {
54+
55+
private static final Logger LOGGER = LoggerFactory.getLogger(ProjectileStrategy.class);
56+
57+
@Override
58+
public void execute() {
59+
LOGGER.info("You shoot the dragon with the magical crossbow and it falls dead on the ground!");
60+
}
61+
}
62+
63+
public class SpellStrategy implements DragonSlayingStrategy {
64+
65+
private static final Logger LOGGER = LoggerFactory.getLogger(SpellStrategy.class);
66+
67+
@Override
68+
public void execute() {
69+
LOGGER.info("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!");
70+
}
71+
}
72+
```
73+
74+
And here is the mighty dragonslayer who is able to pick his fighting strategy based on the opponent.
75+
76+
```java
77+
public class DragonSlayer {
78+
79+
private DragonSlayingStrategy strategy;
80+
81+
public DragonSlayer(DragonSlayingStrategy strategy) {
82+
this.strategy = strategy;
83+
}
84+
85+
public void changeStrategy(DragonSlayingStrategy strategy) {
86+
this.strategy = strategy;
87+
}
88+
89+
public void goToBattle() {
90+
strategy.execute();
91+
}
92+
}
93+
```
94+
95+
Finally here's dragonslayer in action.
96+
97+
```java
98+
LOGGER.info("Green dragon spotted ahead!");
99+
var dragonSlayer = new DragonSlayer(new MeleeStrategy());
100+
dragonSlayer.goToBattle();
101+
LOGGER.info("Red dragon emerges.");
102+
dragonSlayer.changeStrategy(new ProjectileStrategy());
103+
dragonSlayer.goToBattle();
104+
LOGGER.info("Black dragon lands before you.");
105+
dragonSlayer.changeStrategy(new SpellStrategy());
106+
dragonSlayer.goToBattle();
107+
108+
// Green dragon spotted ahead!
109+
// With your Excalibur you sever the dragon's head!
110+
// Red dragon emerges.
111+
// You shoot the dragon with the magical crossbow and it falls dead on the ground!
112+
// Black dragon lands before you.
113+
// You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
114+
```
18115

19116
## Class diagram
20117
![alt text](./etc/strategy_1.png "Strategy")

0 commit comments

Comments
 (0)