CSC/ECE 517 Fall 2012/ch2b 2w40 sn: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 14: | Line 14: | ||
{ | { | ||
public void execute(); | public void execute(); | ||
} | |||
</pre> | |||
<pre> | |||
//Concrete Command | |||
public class LightOnCommand implementsCommand | |||
{ | |||
//reference to the light | |||
Light light; | |||
public LightOnCommand(Light light) | |||
{ | |||
this.light = light; | |||
} | |||
public void execute() | |||
{ | |||
light.switchOn(); | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
//Concrete Command | |||
public class LightOffCommand implementsCommand | |||
{ | |||
//reference to the light | |||
Light light; | |||
public LightOffCommand(Light light) | |||
{ | |||
this.light = light; | |||
} | |||
public void execute() | |||
{ | |||
light.switchOff(); | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
//Receiver | |||
public class Light | |||
{ | |||
private boolean on; | |||
public void switchOn() | |||
{ | |||
on = true; | |||
} | |||
public void switchOff() | |||
{ | |||
on = false; | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
//Invoker | |||
public class RemoteControl | |||
{ | |||
private Command command; | |||
public void setCommand(Command command) | |||
{ | |||
this.command = command; | |||
} | |||
public void pressButton() | |||
{ | |||
command.execute(); | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
//Client | |||
public class Client | |||
{ | |||
public static void main(String[] args) | |||
{ | |||
RemoteControl control = new RemoteControl(); | |||
Light light = new Light(); | |||
Command lightsOn = new LightsOnCommand(light); | |||
Command lightsOff = new LightsOffCommand(light); | |||
//switch on | |||
control.setCommand(lightsOn); | |||
control.pressButton(); | |||
//switch off | |||
control.setCommand(lightsOff); | |||
control.pressButton(); | |||
} | |||
} | } | ||
</pre> | </pre> |
Revision as of 02:19, 17 November 2012
Introduction to Command Pattern
The command pattern is one of the most used behavioral design patterns. The main concept of the pattern is an object which can be used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
The command pattern has been often associated with these terms client, invoker and receiver. The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.
The intent of the Command pattern can be listed as:
- encapsulate a request in an object
- allows the parametrization of clients with different requests
- allows saving the requests in a queue
A Java Example
//Command public interface Command { public void execute(); }
//Concrete Command public class LightOnCommand implementsCommand { //reference to the light Light light; public LightOnCommand(Light light) { this.light = light; } public void execute() { light.switchOn(); } }
//Concrete Command public class LightOffCommand implementsCommand { //reference to the light Light light; public LightOffCommand(Light light) { this.light = light; } public void execute() { light.switchOff(); } }
//Receiver public class Light { private boolean on; public void switchOn() { on = true; } public void switchOff() { on = false; } }
//Invoker public class RemoteControl { private Command command; public void setCommand(Command command) { this.command = command; } public void pressButton() { command.execute(); } }
//Client public class Client { public static void main(String[] args) { RemoteControl control = new RemoteControl(); Light light = new Light(); Command lightsOn = new LightsOnCommand(light); Command lightsOff = new LightsOffCommand(light); //switch on control.setCommand(lightsOn); control.pressButton(); //switch off control.setCommand(lightsOff); control.pressButton(); } }