-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandTest.java
More file actions
62 lines (54 loc) · 1.76 KB
/
CommandTest.java
File metadata and controls
62 lines (54 loc) · 1.76 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
package tests.behavioural;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import patterns.behavioural.command.Command;
import patterns.behavioural.command.ExplorePlan;
import patterns.behavioural.command.GoCommand;
import patterns.behavioural.command.LandCommand;
import patterns.creational.prototype.OrbiterMk2;
import space.Planet;
/**
* COMMAND PATTERN
*
* <p>Purpose: Converts method calls into objects, allowing them to be passed as arguments or switched at runtime.
*
* <p>When to use:
* <ul>
* <li>When you want to parameterize objects with operations</li>
* <li>When you want to queue, schedule, or execute operations remotely</li>
* </ul>
*
* <p>Common Pitfalls:
* <ul>
* <li>Creating too many small command classes</li>
* <li>Not properly handling command parameters</li>
* </ul>
*/
class CommandTest {
/**
* To explore a planet, we need a plan. The plan consists of a series of commands, but we cannot be
* 100% sure which commands we will need, so we need a framework to try different plans.
*
* <p>Use the command pattern to create a framework plan to explore a planet.
*/
@Test
void example() {
var plan = new ExplorePlan();
var orbiter = new OrbiterMk2();
plan.addCommands(new GoCommand(orbiter, Planet.MARS), new LandCommand(orbiter));
plan.execute();
assertThat(plan.getCommands().get(0) instanceof GoCommand).isTrue();
assertThat(plan.getCommands().get(1) instanceof LandCommand).isTrue();
assertThat(plan.getCommands()).allMatch(Command::isComplete);
}
@Test
@Disabled
void todo() {
/*
* todo:
* implement and use your own command
* create a fail-state command
* */
}
}