-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitorTest.java
More file actions
63 lines (56 loc) · 1.89 KB
/
Copy pathVisitorTest.java
File metadata and controls
63 lines (56 loc) · 1.89 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.behavioural;
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.behavioural.visitor.OrbitVisitor;
import patterns.creational.prototype.Orbiter;
import patterns.creational.prototype.OrbiterMk1;
import space.Planet;
/**
* VISITOR PATTERN
*
* <p>Purpose: Separates algorithms from the objects they operate on, allowing new operations without changing the objects.
*
* <p>When to use:
* <ul>
* <li>When you need to perform operations on all elements of a complex object structure</li>
* <li>When you want to add new operations without changing the classes of the elements</li>
* <li>When related operations are grouped in a visitor</li>
* </ul>
*
* <p>Common Pitfalls:
* <ul>
* <li>Adding new element types requires changing all visitors</li>
* <li>Complex implementation in languages without double dispatch</li>
* </ul>
*/
class VisitorTest {
private static List<Orbiter> getOrbiters() {
return List.of(new OrbiterMk1(), new OrbiterMk1());
}
/**
* Other space agencies will want to control our {@link Orbiter}s. We will allow them only certain
* capabilities of our orbiters.
*
* <p>Use the Visitor pattern to implement a way for behavior to be decoupled from the Orbiter
* classes.
*/
@ParameterizedTest
@MethodSource("getOrbiters")
void example(Orbiter orbiter) {
var visitor = new OrbitVisitor(Planet.MARS);
assertThatCode(() -> visitor.visit(orbiter)).doesNotThrowAnyException();
}
@Test
@Disabled
void todo() {
/*
* todo:
* we want to allow other space agencies to control our Landers
* implement a visitor which allows controlled access to our Landers
*/
}
}