Skip to content

Commit bad9214

Browse files
committed
updating to match uml, add to understanding
1 parent c0f498d commit bad9214

File tree

9 files changed

+53
-10
lines changed

9 files changed

+53
-10
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="src" path="test"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
66
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
7+
<classpathentry kind="lib" path="C:/svn/trunk/build/target/ivy/repo-cache/mockito/mockito-all/jars/mockito-all-1.9.5.jar"/>
78
<classpathentry kind="output" path="bin"/>
89
</classpath>

bin/halterman/command/Client.class

-10 Bytes
Binary file not shown.
255 Bytes
Binary file not shown.

bin/halterman/command/Light.class

200 Bytes
Binary file not shown.

bin/halterman/command/Switch.class

594 Bytes
Binary file not shown.

src/halterman/command/Client.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
//Client
44
public class Client {
55
public static void main(String[] args) {
6-
RemoteControl control = new RemoteControl();
6+
7+
//Invoker
8+
Switch lightSwitch = new Switch();
79

10+
//Receiver
811
Light light = new Light();
912

13+
//Concrete Commands from Command Interface
1014
Command lightsOn = new LightOnCommand(light);
1115
Command lightsOff = new LightOffCommand(light);
1216

1317
// switch on
14-
control.setCommand(lightsOn);
15-
control.pressButton();
18+
lightSwitch.setCommand(lightsOn);
19+
lightSwitch.pressButton();
1620

1721
// switch off
18-
control.setCommand(lightsOff);
19-
control.pressButton();
22+
lightSwitch.setCommand(lightsOff);
23+
lightSwitch.pressButton();
2024

2125
}
2226

src/halterman/command/Light.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ public class Light {
66

77
public void switchOn() {
88
on = true;
9+
System.out.println("Turned Light On.");
910
}
1011

1112
public void switchOff() {
1213
on = false;
14+
System.out.println("Turned Light Off.");
1315
}
1416

1517
}

src/halterman/command/Switch.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package halterman.command;
2+
3+
//Invoker
4+
public class Switch {
5+
private Command command;
6+
7+
public void setCommand(Command command) {
8+
this.command = command;
9+
}
10+
11+
public void pressButton() {
12+
command.execute();
13+
}
14+
15+
}

test/halterman/command/CommandPatternTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,38 @@
22

33
import static org.junit.Assert.*;
44

5+
import org.mockito.Mock;
6+
import org.mockito.Mockito;
7+
import org.mockito.MockitoAnnotations;
8+
9+
import org.junit.Before;
510
import org.junit.Test;
611

712
public class CommandPatternTest {
813

14+
@Mock
15+
Light light;
16+
17+
@Mock
18+
Command lightsOn;
19+
20+
private Switch control;
21+
22+
@Before
23+
public void setUp() {
24+
MockitoAnnotations.initMocks(this);
25+
control = new Switch();
26+
}
27+
928
@Test
1029
public void shouldTurnOnLight() {
11-
Light l = new Light();
12-
Command loc = new LightOnCommand(l);
13-
14-
RemoteControl rc = new RemoteControl();
15-
rc.setCommand(loc);
30+
Command lightsOn = new LightOnCommand(light);
31+
32+
// switch on
33+
control.setCommand(lightsOn);
34+
control.pressButton();
35+
36+
//verify
1637

1738
// Need Mockito here, will work on this from home.
1839
}

0 commit comments

Comments
 (0)