Skip to content

Commit 88486f1

Browse files
committed
first commit
0 parents  commit 88486f1

File tree

9 files changed

+124
-0
lines changed

9 files changed

+124
-0
lines changed

.classpath

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>designpatterns</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

src/halterman/command/Client.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package halterman.command;
2+
3+
//Client
4+
public class Client {
5+
public static void main(String[] args) {
6+
RemoteControl control = new RemoteControl();
7+
8+
Light light = new Light();
9+
10+
Command lightsOn = new LightOnCommand(light);
11+
Command lightsOff = new LightOffCommand(light);
12+
13+
// switch on
14+
control.setCommand(lightsOn);
15+
control.pressButton();
16+
17+
// switch off
18+
control.setCommand(lightsOff);
19+
control.pressButton();
20+
21+
}
22+
23+
}

src/halterman/command/Command.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package halterman.command;
2+
3+
//Command
4+
public interface Command {
5+
public void execute();
6+
}

src/halterman/command/Light.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+
//Receiver
4+
public class Light {
5+
private boolean on;
6+
7+
public void switchOn() {
8+
on = true;
9+
}
10+
11+
public void switchOff() {
12+
on = false;
13+
}
14+
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package halterman.command;
2+
3+
//Concrete Command
4+
public class LightOffCommand implements Command
5+
{
6+
//reference to the light
7+
Light light;
8+
9+
public LightOffCommand(Light light)
10+
{
11+
this.light = light;
12+
}
13+
14+
public void execute()
15+
{
16+
light.switchOff();
17+
}
18+
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package halterman.command;
2+
3+
//Concrete Command
4+
public class LightOnCommand implements Command {
5+
// reference to the light
6+
Light light;
7+
8+
public LightOnCommand(Light light) {
9+
this.light = light;
10+
}
11+
12+
public void execute() {
13+
light.switchOn();
14+
}
15+
16+
}
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 RemoteControl {
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package halterman.command;
2+
3+
public class CommandPatternTest {
4+
5+
}

0 commit comments

Comments
 (0)