CSC/ECE 517 Fall 2007/wiki1b 7 c9: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 7: | Line 7: | ||
==Java Implementation== | ==Java Implementation== | ||
<pre> | |||
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( ); | |||
} | |||
} | |||
public class FireEmergencyButton{ | |||
private int onCommandCount = 3; | |||
private int offCommandCount = 2; | |||
private Command [] onCommands; | |||
private Command [] offCommands; | |||
public FireEmergencyButton(){ | |||
onCommands = new Command [onCommandCount]; | |||
offCommands = new Command [offCommandCount]; | |||
} | |||
public void setCommand ( int order, Command on, Command off) { | |||
onCommands[order] = on; // concrete Command registers itself with the invoker | |||
offCommands[order] = off; | |||
} | |||
public void setOnCommand ( int order, Command on) { | |||
onCommands[order] = on; // concrete Command registers itself with the invoker | |||
} | |||
void press( ) { // invoker calls back concrete Command, which executes the Command on the receiver | |||
for(int i=0; i< onCommands.length; i++) | |||
onCommands[i].execute ( ) ; | |||
} | |||
void depress( ) { | |||
for(int i=0; i< offCommands.length; i++) | |||
offCommands[i].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(0, alarmOn, alarmOff); | |||
feb.setCommand(1, wsOn, wsOff); | |||
feb.setOnCommand(2, fsCall); | |||
System.out.println("Fire Emergency Button pressed!"); | |||
feb.press(); | |||
System.out.println(); | |||
System.out.println("Fire Emergency Button depressed!"); | |||
feb.depress(); | |||
} | |||
} | |||
</pre> | |||
Line 45: | Line 178: | ||
puts "Fire Emergency Button is depressed!" | puts "Fire Emergency Button is depressed!" | ||
feb.depress | feb.depress | ||
</pre> | </pre> |
Revision as of 20:09, 1 October 2007
Question
Take a case of the Command pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a "real-world" example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).
Java Implementation
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( ); } } public class FireEmergencyButton{ private int onCommandCount = 3; private int offCommandCount = 2; private Command [] onCommands; private Command [] offCommands; public FireEmergencyButton(){ onCommands = new Command [onCommandCount]; offCommands = new Command [offCommandCount]; } public void setCommand ( int order, Command on, Command off) { onCommands[order] = on; // concrete Command registers itself with the invoker offCommands[order] = off; } public void setOnCommand ( int order, Command on) { onCommands[order] = on; // concrete Command registers itself with the invoker } void press( ) { // invoker calls back concrete Command, which executes the Command on the receiver for(int i=0; i< onCommands.length; i++) onCommands[i].execute ( ) ; } void depress( ) { for(int i=0; i< offCommands.length; i++) offCommands[i].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(0, alarmOn, alarmOff); feb.setCommand(1, wsOn, wsOff); feb.setOnCommand(2, fsCall); System.out.println("Fire Emergency Button pressed!"); feb.press(); System.out.println(); System.out.println("Fire Emergency Button depressed!"); feb.depress(); } }
Ruby Implementation
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