-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateTest.java
More file actions
69 lines (61 loc) · 1.88 KB
/
Copy pathStateTest.java
File metadata and controls
69 lines (61 loc) · 1.88 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
66
67
68
69
package tests.behavioural;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import java.util.UUID;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import patterns.behavioural.state.BasicAntenna;
import patterns.creational.singleton.MissionControl;
/**
* STATE PATTERN
*
* <p>Purpose: Changes object behavior when its state changes by delegating state-specific behavior to separate classes.
*
* <p>When to use:
* <ul>
* <li>When an object's behavior depends on its state</li>
* <li>When you have many conditional statements in methods</li>
* <li>When you want to avoid large state machines</li>
* </ul>
*
* <p>Common Pitfalls:
* <ul>
* <li>Proliferation of state classes</li>
* <li>Not properly handling state transitions</li>
* </ul>
*/
class StateTest {
@AfterAll
static void afterAll() {
MissionControl.contact().getMessages().clear();
}
/**
* Our mission requires a reliable communication system. We need to ensure that the antenna is
* powered on before transmitting any messages.
*
* <p>Use the State pattern to implement the {@link BasicAntenna} class.
*/
@Test
void example() {
var antenna = new BasicAntenna();
UUID earthChannel = MissionControl.contact().getChannel();
assertThatCode(() -> antenna.transmit(earthChannel)).isInstanceOf(Exception.class);
assertThatCode(
() -> {
antenna.powerOn();
antenna.transmit(earthChannel);
antenna.powerOff();
})
.doesNotThrowAnyException();
assertThatCode(() -> antenna.transmit(earthChannel)).isInstanceOf(Exception.class);
}
@Test
@Disabled
void todo() {
/*
* todo:
* we need a stand-by state for the antenna to be able to receive messages
* implement a new state for the antenna
* */
}
}