usingSystem;usingSystem.Collections.Generic;namespaceCommandPattern{publicinterfaceICommand{voidExecute();}/* The Invoker class */publicclassSwitch{privateList<ICommand>_commands=newList<ICommand>();publicvoidStoreAndExecute(ICommandcommand){_commands.Add(command);command.Execute();}}/* The Receiver class */publicclassLight{publicvoidTurnOn(){Console.WriteLine("The light is on");}publicvoidTurnOff(){Console.WriteLine("The light is off");}}/* The Command for turning on the light - ConcreteCommand #1 */publicclassFlipUpCommand:ICommand{privateLight_light;publicFlipUpCommand(Lightlight){_light=light;}publicvoidExecute(){_light.TurnOn();}}/* The Command for turning off the light - ConcreteCommand #2 */publicclassFlipDownCommand:ICommand{privateLight_light;publicFlipDownCommand(Lightlight){_light=light;}publicvoidExecute(){_light.TurnOff();}}/* The test class or client */internalclassProgram{publicstaticvoidMain(string[]args){Lightlamp=newLight();ICommandswitchUp=newFlipUpCommand(lamp);ICommandswitchDown=newFlipDownCommand(lamp);Switchs=newSwitch();stringarg=args.Length>0?args[0].ToUpper():null;if(arg=="ON"){s.StoreAndExecute(switchUp);}elseif(arg=="OFF"){s.StoreAndExecute(switchDown);}else{Console.WriteLine("Argument \"ON\" or \"OFF\" is required.");}}}}
importjava.util.List;importjava.util.ArrayList;/* The Command interface */publicinterfaceCommand{voidexecute();}/* The Invoker class */publicclassSwitch{privateList<Command>history=newArrayList<Command>();publicSwitch(){}publicvoidstoreAndExecute(Commandcmd){this.history.add(cmd);// optional cmd.execute();}}/* The Receiver class */publicclassLight{publicLight(){}publicvoidturnOn(){System.out.println("The light is on");}publicvoidturnOff(){System.out.println("The light is off");}}/* The Command for turning on the light - ConcreteCommand #1 */publicclassFlipUpCommandimplementsCommand{privateLighttheLight;publicFlipUpCommand(Lightlight){this.theLight=light;}publicvoidexecute(){theLight.turnOn();}}/* The Command for turning off the light - ConcreteCommand #2 */publicclassFlipDownCommandimplementsCommand{privateLighttheLight;publicFlipDownCommand(Lightlight){this.theLight=light;}publicvoidexecute(){theLight.turnOff();}}/* The test class or client */publicclassPressSwitch{publicstaticvoidmain(String[]args){Lightlamp=newLight();CommandswitchUp=newFlipUpCommand(lamp);CommandswitchDown=newFlipDownCommand(lamp);SwitchmySwitch=newSwitch();try{if("ON".equalsIgnoreCase(args[0])){mySwitch.storeAndExecute(switchUp);}elseif("OFF".equalsIgnoreCase(args[0])){mySwitch.storeAndExecute(switchDown);}else{System.out.println("Argument \"ON\" or \"OFF\" is required.");}}catch(Exceptione){System.out.println("Arguments required.");}}}
classSwitch(object):"""The INVOKER class"""def__init__(self,flip_up_cmd,flip_down_cmd):self.flip_up=flip_up_cmdself.flip_down=flip_down_cmdclassLight(object):"""The RECEIVER class"""defturn_on(self):print("The light is on")defturn_off(self):print("The light is off")classLightSwitch(object):"""The CLIENT class"""def__init__(self):lamp=Light()self._switch=Switch(lamp.turn_on,lamp.turn_off)defswitch(self,cmd):cmd=cmd.strip().upper()ifcmd=="ON":self._switch.flip_up()elifcmd=="OFF":self._switch.flip_down()else:print("Argument 'ON' or 'OFF' is required.")# Execute if this file is run as a script and not imported as a moduleif__name__=="__main__":light_switch=LightSwitch()print("Switch ON test.")light_switch.switch("ON")print("Switch OFF test.")light_switch.switch("OFF")print("Invalid Command test.")light_switch.switch("****")
/* The Command interface */traitCommand{defexecute()}/* The Invoker class */classSwitch{privatevarhistory:List[Command]=NildefstoreAndExecute(cmd:Command){cmd.execute()this.history:+=cmd}}/* The Receiver class */classLight{defturnOn()=println("The light is on")defturnOff()=println("The light is off")}/* The Command for turning on the light - ConcreteCommand #1 */classFlipUpCommand(theLight:Light)extendsCommand{defexecute()=theLight.turnOn()}/* The Command for turning off the light - ConcreteCommand #2 */classFlipDownCommand(theLight:Light)extendsCommand{defexecute()=theLight.turnOff()}/* The test class or client */objectPressSwitch{defmain(args:Array[String]){vallamp=newLight()valswitchUp=newFlipUpCommand(lamp)valswitchDown=newFlipDownCommand(lamp)vals=newSwitch()try{args(0).toUpperCasematch{case"ON"=>s.storeAndExecute(switchUp)case"OFF"=>s.storeAndExecute(switchDown)case_=>println("Argument \"ON\" or \"OFF\" is required.")}}catch{casee:Exception=>println("Arguments required.")}}}
/* The Invoker function */varSwitch=function(){var_commands=[];this.storeAndExecute=function(command){_commands.push(command);command.execute();}}/* The Receiver function */varLight=function(){this.turnOn=function(){console.log('turn on')};this.turnOff=function(){console.log('turn off')};}/* The Command for turning on the light - ConcreteCommand #1 */varFlipUpCommand=function(light){this.execute=light.turnOn;}/* The Command for turning off the light - ConcreteCommand #2 */varFlipDownCommand=function(light){this.execute=light.turnOff;}varlight=newLight();varswitchUp=newFlipUpCommand(light);varswitchDown=newFlipDownCommand(light);vars=newSwitch();s.storeAndExecute(switchUp);s.storeAndExecute(switchDown);
classICommand{public:virtualvoidExecute()=0;};classSwicher{private:std::vector<ICommand*>_commands;public:voidStoreAndExecute(ICommand*command){if(command){_commands.push_back(command);command->Execute();}}};classLight{public:voidTurnOn(){std::cout<<"The light is on."<<std::endl;}voidTurnOff(){std::cout<<"The light is off."<<std::endl;}};/* The Command for turning on the light - ConcreteCommand #1 */classFlipUpCommand:publicICommand{private:Light*_light;public:FlipUpCommand(Light*light){_light=light;}voidExecute(){_light->TurnOn();}};/* The Command for turning off the light - ConcreteCommand #2 */classFlipDownCommand:publicICommand{private:Light*_light;public:FlipDownCommand(Light*light){_light=light;}voidExecute(){_light->TurnOff();}};intmain(){Light*light=newLight();ICommand*switchOn=dynamic_cast<ICommand*>(newFlipUpCommand(light));ICommand*switchDown=dynamic_cast<ICommand*>(newFlipDownCommand(light));Swicher*switcher=newSwicher();switcher->StoreAndExecute(switchOn);switcher->StoreAndExecute(switchDown);deleteswitcher;deleteswitchOn;deleteswitchDown;deletelight;}