-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeTest.java
More file actions
63 lines (58 loc) · 2 KB
/
CompositeTest.java
File metadata and controls
63 lines (58 loc) · 2 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
package tests.structural;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import java.util.List;
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.creational.prototype.Orbiter;
import patterns.creational.prototype.OrbiterMk2;
import patterns.structural.composite.Fleet;
import patterns.structural.composite.Squad;
import space.Planet;
/**
* COMPOSITE PATTERN
*
* <p>Purpose: Creates tree structures of objects, allowing uniform handling of both individual objects and compositions.
*
* <p>When to use:
* <ul>
* <li>When you want to represent part-whole hierarchies</li>
* <li>When clients should be able to ignore differences between compositions and individual objects</li>
* <li>When you need to work with recursive structures</li>
* </ul>
*
* <p>Common Pitfalls:
* <ul>
* <li>Not properly handling operations that only make sense for certain components</li>
* <li>Performance issues with deep hierarchies</li>
* </ul>
*/
class CompositeTest {
/**
* We have a fleet of rovers that need to explore the solar system. The fleet consists of squads
* of rovers. Each squad has a team of rovers.
*
* <p>Use the composite pattern to command fleets of {@link Orbiter}s.
*
* @param planet to explore
*/
@ParameterizedTest
@MethodSource("space.Planet#values")
void example(Planet planet) {
var fleet = new Fleet();
fleet.add(new Squad(List.of(new OrbiterMk2(), new OrbiterMk2(), new OrbiterMk2())));
fleet.add(new Squad(List.of(new OrbiterMk2(), new OrbiterMk2(), new OrbiterMk2())));
assertThatCode(() -> fleet.explore(planet)).doesNotThrowAnyException();
assertThat(fleet.count()).isEqualTo(6);
}
@Test
@Disabled
void todo() {
/*
* todo:
* add ability to combine Fleets into larger Fleets
* */
}
}