-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyTest.java
More file actions
65 lines (59 loc) · 1.73 KB
/
Copy pathStrategyTest.java
File metadata and controls
65 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tests.behavioural;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import patterns.behavioural.strategy.Lidar;
import patterns.behavioural.strategy.Radar;
import patterns.behavioural.strategy.Sensor;
import space.Planet;
/**
* STRATEGY PATTERN
*
* <p>Purpose: Defines a family of interchangeable algorithms that can be switched at runtime.
*
* <p>When to use:
*
* <ul>
* <li>When you want to define a family of algorithms
* <li>When you need different variants of an algorithm
* <li>When you want to avoid exposing algorithm-specific data structures
* </ul>
*
* <p>Common Pitfalls:
*
* <ul>
* <li>Creating strategies for simple algorithms
* <li>Not properly handling strategy parameters
* </ul>
*/
class StrategyTest {
/**
* Our mission requires a sensor to analyze the surface of each planet. The sensor must use a
* radar to analyze planets with a max temperature above 150 Kelvin and a lidar for the rest.
*
* @param planet to analyze
*/
@ParameterizedTest
@MethodSource("space.Planet#values")
void example(Planet planet) {
var sensor = new Sensor();
if (planet.getMaxTempKelvin() > 150) {
sensor.setStrategy(new Radar());
} else {
sensor.setStrategy(new Lidar());
}
assertThatCode(() -> sensor.analyze(planet)).doesNotThrowAnyException();
}
@Test
@Disabled
void todo() {
/*
* todo:
* rings represent a hazard to orbiters
* create a new strategy for planets with rings
* create a special strategy just for Earth
* */
}
}