-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyweightTest.java
86 lines (80 loc) · 2.19 KB
/
FlyweightTest.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package tests.structural;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import patterns.structural.flyweight.OrbiterCache;
import space.Planet;
/**
* FLYWEIGHT PATTERN is a structural design pattern that aims to minimize memory usage or
* computational expenses by sharing as much data as possible with other similar objects.
*/
public class FlyweightTest {
/**
* When exploring space, we must be very careful with our resources. However, space is the great
* unknown so we don't exactly know what to expect. We need to be able to adapt to potentially repeating situations
* and create orbiters on demand.
*
* <p>Use the flyweight pattern to create and store orbiters for various missions.
*
* @param planet to orbit
*/
@ParameterizedTest
@MethodSource("space.Planet#values")
void flyweightPattern(Planet planet) {
unforeseeableMissions().forEach(mission -> OrbiterCache.getOrCreate(mission).doOrbit(planet));
OrbiterCache.getOrCreate("dive").doOrbit(planet);
assertThat(OrbiterCache.countOrbiters()).isEqualTo(7);
}
private List<String> unforeseeableMissions() {
return List.of(
"equator",
"moon",
"northPole",
"highOrbit",
"southPole",
"lowOrbit",
"highOrbit",
"equator",
"moon",
"northPole",
"southPole",
"lowOrbit",
"northPole",
"highOrbit",
"lowOrbit",
"moon",
"southPole",
"equator",
"southPole",
"lowOrbit",
"moon",
"northPole",
"highOrbit",
"equator",
"moon",
"equator",
"southPole",
"lowOrbit",
"highOrbit",
"northPole",
"lowOrbit",
"moon",
"northPole",
"southPole",
"equator",
"highOrbit",
"equator",
"northPole",
"highOrbit",
"southPole",
"lowOrbit",
"moon",
"moon",
"southPole",
"equator",
"highOrbit",
"northPole",
"lowOrbit");
}
}