CSC/ECE 517 Fall 2007/wiki1b 7 c9: Difference between revisions
Line 113: | Line 113: | ||
public class FireEmergencyButton{ | public class FireEmergencyButton{ | ||
ArrayList <Command> onCommands = new ArrayList <Command>(); | |||
ArrayList <Command> offCommands = new ArrayList <Command>(); | ArrayList <Command> offCommands = new ArrayList <Command>(); | ||
public void setCommand (Command on, Command off) { | |||
onCommands.add(on); | |||
offCommands.add(off); | |||
} | |||
public void setOnCommand (Command on) { | |||
onCommands.add(on); | |||
} | |||
public void press() { | |||
for(Command c : onCommands) | |||
c.execute(); | |||
} | |||
public void depress() { | |||
for(Command c : offCommands) | |||
c.execute(); | |||
} | |||
} | } | ||
public class TestCommand { | public class TestCommand { | ||
public static void main(String[] args) { | |||
WaterSprayer ws = new WaterSprayer( ); | |||
WaterSprayerOnCommand wsOn = new WaterSprayerOnCommand (ws); | |||
WaterSprayerOffCommand wsOff = new WaterSprayerOffCommand (ws); | |||
FireAlarm a = new FireAlarm( ); | |||
FireAlarmOnCommand alarmOn = new FireAlarmOnCommand (a); | |||
FireAlarmOffCommand alarmOff = new FireAlarmOffCommand (a); | |||
FireStation fs = new FireStation( ); | |||
FireStationCallCommand fsCall = new FireStationCallCommand(fs); | |||
FireEmergencyButton feb = new FireEmergencyButton(); | |||
feb.setCommand(alarmOn, alarmOff); | |||
feb.setCommand(wsOn, wsOff); | |||
feb.setOnCommand(fsCall); | |||
System.out.println("Fire Emergency Button pressed!"); | System.out.println("Fire Emergency Button pressed!"); | ||
feb.press(); | |||
System.out.println(); | |||
System.out.println("Fire Emergency Button depressed!"); | |||
feb.depress(); | feb.depress(); | ||
} | |||
} | } | ||
</pre> | </pre> |
Revision as of 00:49, 5 October 2007
Command Pattern
What is Command Pattern?
Command pattern is a design pattern in which you can encapsulate actions/requests as objects and parametrize other objects with different requests. By this way a program (or simply an object) can request actions and that object will not worry how the computation of the request is made. It allows the program to decouple the requester(client) of an action from the object that performs the action(receiver).
Components of Command Pattern
(The following UML diagram is taken from the book Design Patterns Elements of Reusable Object-Oriented Software)
- Command: <<interface>> declares an interface for all commands.
- ConcreteCommand: implements Command interface, defines a binding between a Receiver and an action, and implements execute() method by invoking the corresponding action(s) on Receiver.
- Client: creates a ConcreteCommand object and sets its receiver.
- Invoker: asks the command to carry out the request by calling execute() method of ConcreteCommand.
- Receiver: knows how to perform the operations associated with carrying out a request.
Advantages of Command Pattern
- A command object is a convenient temporary storage for procedure parameters. A command can be set aside for later use.
- As we can save commands we can also use these with data structures, and we can call the actions one after another from a queue with a specific order we want or according to their priorities.
- As it treats the commands as objects, command pattern supports undo-able operations, provided that the operations are stored in a data structure.
- It helps making the code modular.
In some cases instead of using Command pattern one can use switches. But this is bad design because every time we need to call for an action we need to use switch statements. Furthermore, if we need to change one of the switch cases or write a new case we have to change the code in every switch statement. This is also against the object oriented design of our program because the caller will know how the action it is requesting is implemented.
Uses of Command Pattern
- Command pattern is widely used for saving actions (logging) and for “undo” operations.
- In networking command patterns can be used while passing actions from one computer to another.
- One of its most important area of use is GUI buttons. When a button is pressed a command could be sent to the main program.
- Also Command pattern could be used when some actions are needed to be called together. This is called a macro and as an example we can give commands saved in an array and calling them when a particular situation invokes them all.
Implementations of Command Pattern
We are using command pattern to simulate a fire emergency scenario. In this example, when the fire emergency button is pressed, the fire alarm should be started, the water sprayers should spray water and the fire department should be called. When the fire emergency button is depressed, the fire alarm should stop, and water sprayers should stop spraying water.
Java Implementation
In our case, the fire emergency button is the Invoker, and it has no idea what happens when the execute() method is called. FireAlarmOnCommand, FireAlarmOffCommand, WaterSprayerOnCommand, WaterSprayerOffCommand and FireStationCallCommand are ConcreteCommands which implement the Command interface. Their execute() methods invokes the actions on the appropriate Receivers to fulfill the request. FireStation, FireAlarm and WaterSprayer are the Receivers they know how to perform the action requested, in our case they simply print out what they should be doing.
public interface Command { public abstract void execute ( ); } public class FireAlarm { public void turnOn( ) { System.out.println("Fire alarm is on!"); } public void turnOff( ) { System.out.println("Fire alarm is off..."); } } public class FireAlarmOffCommand implements Command { private FireAlarm myAlarm; public FireAlarmOffCommand (FireAlarm a) { myAlarm = a; } public void execute( ) { myAlarm.turnOff( ); } } public class FireAlarmOnCommand implements Command { private FireAlarm myAlarm; public FireAlarmOnCommand (FireAlarm a) { myAlarm = a; } public void execute( ) { myAlarm.turnOn( ); } } public class FireStation { public void call( ) { System.out.println("Fire department is called!"); } } public class FireStationCallCommand implements Command { private FireStation myStation; public FireStationCallCommand (FireStation fs) { myStation = fs; } public void execute() { myStation.call(); } } public class WaterSprayer { public void startSprayWater() { System.out.println("Started spraying water!"); } public void stopSprayWater() { System.out.println("Stopped spraying water..."); } } public class WaterSprayerOffCommand implements Command { private WaterSprayer myWaterSprayer; public WaterSprayerOffCommand (WaterSprayer ws) { myWaterSprayer = ws; } public void execute() { myWaterSprayer.stopSprayWater( ); } } public class WaterSprayerOnCommand implements Command { private WaterSprayer myWaterSprayer; public WaterSprayerOnCommand (WaterSprayer ws) { myWaterSprayer = ws; } public void execute() { myWaterSprayer.startSprayWater( ); } } import java.util.ArrayList; public class FireEmergencyButton{ ArrayList <Command> onCommands = new ArrayList <Command>(); ArrayList <Command> offCommands = new ArrayList <Command>(); public void setCommand (Command on, Command off) { onCommands.add(on); offCommands.add(off); } public void setOnCommand (Command on) { onCommands.add(on); } public void press() { for(Command c : onCommands) c.execute(); } public void depress() { for(Command c : offCommands) c.execute(); } } public class TestCommand { public static void main(String[] args) { WaterSprayer ws = new WaterSprayer( ); WaterSprayerOnCommand wsOn = new WaterSprayerOnCommand (ws); WaterSprayerOffCommand wsOff = new WaterSprayerOffCommand (ws); FireAlarm a = new FireAlarm( ); FireAlarmOnCommand alarmOn = new FireAlarmOnCommand (a); FireAlarmOffCommand alarmOff = new FireAlarmOffCommand (a); FireStation fs = new FireStation( ); FireStationCallCommand fsCall = new FireStationCallCommand(fs); FireEmergencyButton feb = new FireEmergencyButton(); feb.setCommand(alarmOn, alarmOff); feb.setCommand(wsOn, wsOff); feb.setOnCommand(fsCall); System.out.println("Fire Emergency Button pressed!"); feb.press(); System.out.println(); System.out.println("Fire Emergency Button depressed!"); feb.depress(); } }
Ruby Implementation
Command Pattern is implemented in Ruby by using Proc objects, a Proc object represents a callable block of code that closes over the variables in scope when it was created. Fire Emergency Button has to sound the alarm, start the water sprayer and call the fire department when it is pressed and it should shut down the alarm, stop the water sprayer when it is depressed. So here in our Ruby implementation, a Fire Emergency Button class takes onCommands and offCommands (which are array of Proc objects) as parameters when it is being initialized and simply calls onCommands when it is pressed and offCommands when it is depressed.
class FireEmergencyButton def initialize(onCommands, offCommands) @onCommands = onCommands @offCommands = offCommands end def press @onCommands.each{|command| command.call} end def depress @offCommands.each{|command| command.call} end end waterSprayOnCommand = proc{puts "Started spraying water!"} waterSprayOffCommand = proc {puts "Stopped spraying water..."} fireAlarmOnCommand = proc{puts "Fire alarm is on!"} fireAlarmOffCommand = proc {puts "Fire alarm is off..."} fireStationCallCommand = proc{puts "Fire department is called!"} onCommands = [] onCommands << fireAlarmOnCommand onCommands << waterSprayOnCommand onCommands << fireStationCallCommand offCommands = [] offCommands << fireAlarmOffCommand offCommands << waterSprayOffCommand feb =FireEmergencyButton.new(onCommands, offCommands) puts "Fire Emergency Button is pressed!" feb.press puts "" puts "Fire Emergency Button is depressed!" feb.depress
Output of Implementations
Both Java and Ruby implementation of the command pattern prints out the following:
Fire Emergency Button pressed! Fire alarm is on! Started spraying water! Fire department is called! Fire Emergency Button depressed! Fire alarm is off... Stopped spraying water...
Comparison of Java and Ruby Implementations
- Java is more verbose than Ruby. Even though both Java and Ruby code implement the same thing, our Java code is twice as much as longer than our Ruby code. This is basically because the Ruby Proc objects are more concise and convenient than Command objects as implemented in Java.
- On the other hand, Java code is more modular when compared to the Ruby code. Everything in Java is created within classes and that makes Java code easy to understand especially for unexperienced users.
- In Java an interface declaration is needed to implement a Command pattern whereas in Ruby this is not needed because Ruby has Proc objects built in.
- In Java all the commands need to be defined in classes, Ruby can do this in a single line by using the keyword Proc.
- Ruby's built in iterators makes executing commands from an array a lot more easier. Arraylist in Java has a foreach form to this.
- Adding new commands to our command array at runtime is possible with Ruby. Java is a static language and meta programming is not possible. Even though we can add objects at runtime to Java Arraylist we can not create runtime commands.
As a result we can say that Ruby is more succinct than Java. We can see that implementing a lot of things in Ruby is easier and shorter than Java. But Java being more modular, it makes it easier for someone trying to understand the code written. The results show that experienced users would prefer Ruby for implementing command pattern.
References
- Ruby: Command Pattern
- Lecture notes provided by Dr. Gehringer on Command Pattern [1]
- Wikipedia Command Pattern
- Learn How to Implement Command Pattern in Java
- Ruby for the Java World
- Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides Design Patterns Elements of Reusable Object-Oriented Software Addison-Wesley, 1995
- Eric Freeman, and Elisabeth Freeman Head First Design Patterns O'Reilly 2004