CSC/ECE 517 Fall 2007/wiki1b 7 c9: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 9: Line 9:
<pre>
<pre>
public interface Command {
public interface Command {
public abstract void execute ( );
    public abstract void execute ( );
}
}


public class FireAlarm {
public class FireAlarm {
public void turnOn( ) {
    public void turnOn( ) {
         System.out.println("Fire alarm is on!");
         System.out.println("Fire alarm is on!");
}
    }
public void turnOff( ) {
    public void turnOff( ) {
System.out.println("Fire alarm is off...");
System.out.println("Fire alarm is off...");
}
    }
 
}
}


Line 43: Line 42:


public class FireStation {
public class FireStation {
public void call( ) {
    public void call( ) {
         System.out.println("Fire department is called!");
         System.out.println("Fire department is called!");
}
    }
}
}


Line 59: Line 58:


public class WaterSprayer {
public class WaterSprayer {
public void startSprayWater() {
    public void startSprayWater() {
System.out.println("Started spraying water!");
System.out.println("Started spraying water!");
}
    }
public void stopSprayWater() {
    public void stopSprayWater() {
System.out.println("Stopped spraying water...");
System.out.println("Stopped spraying water...");
}
    }
}
}


Line 88: Line 87:


public class  FireEmergencyButton{
public class  FireEmergencyButton{
private int onCommandCount = 3;
    private int onCommandCount = 3;
private int offCommandCount = 2;
    private int offCommandCount = 2;
     private Command [] onCommands;  
     private Command [] onCommands;  
     private Command [] offCommands;
     private Command [] offCommands;
Line 97: Line 96:
     }
     }
     public void setCommand ( int order, Command on, Command off) {
     public void setCommand ( int order, Command on, Command off) {
     onCommands[order] = on; // concrete Command registers itself with the invoker
     onCommands[order] = on;  
         offCommands[order] = off;
         offCommands[order] = off;
     }
     }
     public void setOnCommand ( int order, Command on) {
     public void setOnCommand ( int order, Command on) {
         onCommands[order] = on; // concrete Command registers itself with the invoker
         onCommands[order] = on;  
     }
     }
     void press( ) { // invoker calls back concrete Command, which executes the Command on the receiver
     void press() {  
     for(int i=0; i< onCommands.length; i++)  
     for(int i=0; i< onCommands.length; i++)  
    onCommands[i].execute ( ) ;                           
        onCommands[i].execute() ;                           
     }
     }
     void depress( ) {
     void depress() {
     for(int i=0; i< offCommands.length; i++)
     for(int i=0; i< offCommands.length; i++)
    offCommands[i].execute ( );
        offCommands[i].execute ( );
     }
     }
}
}


public class TestCommand {
public class TestCommand {
public static void main(String[] args) {
    public static void main(String[] args) {
WaterSprayer  ws = new WaterSprayer( );
        WaterSprayer  ws = new WaterSprayer();
WaterSprayerOnCommand wsOn = new WaterSprayerOnCommand (ws);
        WaterSprayerOnCommand wsOn = new WaterSprayerOnCommand (ws);
WaterSprayerOffCommand wsOff = new WaterSprayerOffCommand (ws);
        WaterSprayerOffCommand wsOff = new WaterSprayerOffCommand (ws);


FireAlarm a = new FireAlarm( );
        FireAlarm a = new FireAlarm();
FireAlarmOnCommand alarmOn = new FireAlarmOnCommand (a);
        FireAlarmOnCommand alarmOn = new FireAlarmOnCommand (a);
FireAlarmOffCommand alarmOff = new FireAlarmOffCommand (a);
        FireAlarmOffCommand alarmOff = new FireAlarmOffCommand (a);
FireStation fs = new FireStation( );
        FireStation fs = new FireStation( );
FireStationCallCommand fsCall = new FireStationCallCommand(fs);
        FireStationCallCommand fsCall = new FireStationCallCommand(fs);
             
             
FireEmergencyButton feb = new FireEmergencyButton();
        FireEmergencyButton feb = new FireEmergencyButton();
feb.setCommand(0, alarmOn, alarmOff);
        feb.setCommand(0, alarmOn, alarmOff);
feb.setCommand(1, wsOn, wsOff);
        feb.setCommand(1, wsOn, wsOff);
feb.setOnCommand(2, fsCall);   
        feb.setOnCommand(2, fsCall);   
       
         System.out.println("Fire Emergency Button pressed!");
         System.out.println("Fire Emergency Button pressed!");
feb.press();
        feb.press();
        System.out.println();
System.out.println();
        System.out.println("Fire Emergency Button depressed!");
System.out.println("Fire Emergency Button depressed!");
         feb.depress();
         feb.depress();
}  
    }  
}  
}  
</pre>
</pre>

Revision as of 20:14, 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; 
        offCommands[order] = off;
    }
    public void setOnCommand ( int order, Command on) {
        onCommands[order] = on; 
    }
    void press() { 
    	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