Client.java /** * */ /** * @author Mukesh * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)" */ public class Client { }
Command.java public interface Command { public void execute(); }
ConcreteCommand.java
/** * */ /** * @author Mukesh * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)" */ public class ConcreteCommand { }
Light.java public class Light { private Object Location; public Object getLocation() { return Location; } public void setLocation(Object theLocation) { Location = theLocation; } private Boolean LightState; public Boolean getLightState() { return LightState; } public void setLightState(Boolean theLightState) { LightState = theLightState; } public Light(String location) { this.Location = location; } public Light() { } public void On() { if (LightState == false) { System.out.println("The light is now on for:" + this.Location); LightState = true; } else System.out.println("The light is already on for:" + this.Location); } public void Off() { if (LightState == true) { System.out.println("The light is now off for:" + Location); } else System.out.println("The light is already off for:" + Location); } }
LightOff.java public class LightOff implements Command { /** * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)" */ public Light receiver; /** * @return the receiver * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)" */ public Light getReceiver() { // begin-user-code return receiver; // end-user-code } /** * @param theReceiver the receiver to set * @generated "UML to Java V5.0 (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)" */ public void setReceiver(Light theReceiver) { // begin-user-code receiver = theReceiver; // end-user-code } /** * @generated "Command (com.ibm.xtools.patterns.content.gof.behavioral.command.CommandPattern)" */ public LightOff(Light receiver) { // begin-user-code this.receiver = receiver; }
// end-user-code
/** * @generated "Command (com.ibm.xtools.patterns.content.gof.behavioral.command.CommandPattern)" */ public void execute() { // begin-user-code // begin-user-code// TODO: Invoke methods in receiver to carry out the request. throw new java.lang.UnsupportedOperationException();
} }
// end-user-code // end-user-code
Lighton.java public class LightOn implements Command { public Light receiver; public Light getReceiver() { return receiver; } public void setReceiver(Light theReceiver) { receiver = theReceiver; } public LightOn(Light receiver) { this.receiver = receiver; }
}
public void execute() { receiver.On(); }
Person.jva public class Person { public void invoke() { Light aReceiver = new Light(); LightOn aConcreteCommand = new LightOn(aReceiver); Switch anInvoker = new Switch(); anInvoker.setCommand(aConcreteCommand); anInvoker.invokeCommand(); } public static void main(String[] args) { Light aReceiver = new Light("My Room"); LightOn aconcom = new LightOn(aReceiver); LightOff bconcom = new LightOff(aReceiver); Switch anInvoker = new Switch(); anInvoker.setFlipUp(aconcom); anInvoker.setFlipDown(bconcom); for (int i = 0; i < args.length; i++) { if (args[i].equalsIgnoreCase("on")) anInvoker.FlipUp(); else if (args[i].equalsIgnoreCase("off")) anInvoker.FlipDown(); } } }
Switch.java public class Switch { private Command command; public Command getCommand() { return command; } private Command FlipDown; private Command FlipUp; public Command getFlipDown() { return FlipDown; } public void setFlipDown(Command theFlipDown) { FlipDown = theFlipDown; } public Command getFlipUp() { return FlipUp; } public void setFlipUp(Command theFlipUp) { FlipUp = theFlipUp; } public void FlipDown() { FlipDown.execute(); } public void FlipUp() { FlipUp.execute(); } public void setCommand(Command theCommand) { this.command = theCommand; } public void invokeCommand() { this.command.execute(); } }