命令模式(实例二)
来源:互联网 发布:js播放器代码 编辑:程序博客网 时间:2024/06/04 18:48
RECERVER
public class CeilingFan {String location = "";int level;public static final int HIGH = 2;public static final int MEDIUM = 1;public static final int LOW = 0; public CeilingFan(String location) {this.location = location;} public void high() {// turns the ceiling fan on to highlevel = HIGH;System.out.println(location + " ceiling fan is on high"); } public void medium() {// turns the ceiling fan on to mediumlevel = MEDIUM;System.out.println(location + " ceiling fan is on medium");}public void low() {// turns the ceiling fan on to lowlevel = LOW;System.out.println(location + " ceiling fan is on low");} public void off() {// turns the ceiling fan offlevel = 0;System.out.println(location + " ceiling fan is off");} public int getSpeed() {return level;}}
public class GarageDoor {String location;public GarageDoor(String location) {this.location = location;}public void up() {System.out.println(location + " garage Door is Up");}public void down() {System.out.println(location + " garage Door is Down");}public void stop() {System.out.println(location + " garage Door is Stopped");}public void lightOn() {System.out.println(location + " garage light is on");}public void lightOff() {System.out.println(location + " garage light is off");}}
public class Hottub {boolean on;int temperature;public Hottub() {}public void on() {on = true;}public void off() {on = false;}public void bubblesOn() {if (on) {System.out.println("Hottub is bubbling!");}}public void bubblesOff() {if (on) {System.out.println("Hottub is not bubbling");}}public void jetsOn() {if (on) {System.out.println("Hottub jets are on");}}public void jetsOff() {if (on) {System.out.println("Hottub jets are off");}}public void setTemperature(int temperature) {this.temperature = temperature;}public void heat() {temperature = 105;System.out.println("Hottub is heating to a steaming 105 degrees");}public void cool() {temperature = 98;System.out.println("Hottub is cooling to 98 degrees");}}
public class Light {String location = "";public Light(String location) {this.location = location;}public void on() {System.out.println(location + " light is on");}public void off() {System.out.println(location + " light is off");}}
public class Stereo {String location;public Stereo(String location) {this.location = location;}public void on() {System.out.println(location + " stereo is on");}public void off() {System.out.println(location + " stereo is off");}public void setCD() {System.out.println(location + " stereo is set for CD input");}public void setDVD() {System.out.println(location + " stereo is set for DVD input");}public void setRadio() {System.out.println(location + " stereo is set for Radio");}public void setVolume(int volume) {// code to set the volume// valid range: 1-11 (after all 11 is better than 10, right?)System.out.println(location + " Stereo volume set to " + volume);}}
public class TV {String location;int channel;public TV(String location) {this.location = location;}public void on() {System.out.println("TV is on");}public void off() {System.out.println("TV is off");}public void setInputChannel() {this.channel = 3;System.out.println("Channel is set for VCR");}}
COMMAND
public interface Command {public void execute();}
public class CeilingFanOffCommand implements Command {CeilingFan ceilingFan;public CeilingFanOffCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {ceilingFan.off();}}
public class CeilingFanOnCommand implements Command {CeilingFan ceilingFan;public CeilingFanOnCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {ceilingFan.high();}}
public class GarageDoorDownCommand implements Command {GarageDoor garageDoor;public GarageDoorDownCommand(GarageDoor garageDoor) {this.garageDoor = garageDoor;}public void execute() {garageDoor.up();}}
public class GarageDoorUpCommand implements Command {GarageDoor garageDoor;public GarageDoorUpCommand(GarageDoor garageDoor) {this.garageDoor = garageDoor;}public void execute() {garageDoor.up();}}
public class HottubOffCommand implements Command {Hottub hottub;public HottubOffCommand(Hottub hottub) {this.hottub = hottub;}public void execute() {hottub.cool();hottub.off();}}
public class HottubOnCommand implements Command {Hottub hottub;public HottubOnCommand(Hottub hottub) {this.hottub = hottub;}public void execute() {hottub.on();hottub.heat();hottub.bubblesOn();}}
public class LightOffCommand implements Command {Light light; public LightOffCommand(Light light) {this.light = light;} public void execute() {light.off();}}
public class LightOnCommand implements Command {Light light;public LightOnCommand(Light light) {this.light = light;}public void execute() {light.on();}}
public class LivingroomLightOffCommand implements Command {Light light;public LivingroomLightOffCommand(Light light) {this.light = light;}public void execute() {light.off();}}
public class LivingroomLightOnCommand implements Command {Light light;public LivingroomLightOnCommand(Light light) {this.light = light;}public void execute() {light.on();}}
public class NoCommand implements Command {public void execute() { }}
public class StereoOffCommand implements Command {Stereo stereo; public StereoOffCommand(Stereo stereo) {this.stereo = stereo;} public void execute() {stereo.off();}}
public class StereoOnWithCDCommand implements Command {Stereo stereo; public StereoOnWithCDCommand(Stereo stereo) {this.stereo = stereo;} public void execute() {stereo.on();stereo.setCD();stereo.setVolume(11);}}
INVOKEF
public class RemoteControl {Command[] onCommands;Command[] offCommands; public RemoteControl() {onCommands = new Command[7];offCommands = new Command[7]; Command noCommand = new NoCommand();for (int i = 0; i < 7; i++) {onCommands[i] = noCommand;offCommands[i] = noCommand;}} public void setCommand(int slot, Command onCommand, Command offCommand) {onCommands[slot] = onCommand;offCommands[slot] = offCommand;} public void onButtonWasPushed(int slot) {onCommands[slot].execute();} public void offButtonWasPushed(int slot) {offCommands[slot].execute();} public String toString() {StringBuffer stringBuff = new StringBuffer();stringBuff.append("\n------ Remote Control -------\n");for (int i = 0; i < onCommands.length; i++) {stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()+ " " + offCommands[i].getClass().getName() + "\n");}return stringBuff.toString();}}
CLIENT
public class RemoteLoader { public static void main(String[] args) {RemoteControl remoteControl = new RemoteControl(); Light livingRoomLight = new Light("Living Room");Light kitchenLight = new Light("Kitchen");CeilingFan ceilingFan= new CeilingFan("Living Room");GarageDoor garageDoor = new GarageDoor("");Stereo stereo = new Stereo("Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorUpCommand garageDoorUp =new GarageDoorUpCommand(garageDoor);GarageDoorDownCommand garageDoorDown =new GarageDoorDownCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCD =new StereoOnWithCDCommand(stereo);StereoOffCommand stereoOff =new StereoOffCommand(stereo); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);remoteControl.setCommand(3, stereoOnWithCD, stereoOff); System.out.println(remoteControl); remoteControl.onButtonWasPushed(0);remoteControl.offButtonWasPushed(0);remoteControl.onButtonWasPushed(1);remoteControl.offButtonWasPushed(1);remoteControl.onButtonWasPushed(2);remoteControl.offButtonWasPushed(2);remoteControl.onButtonWasPushed(3);remoteControl.offButtonWasPushed(3);}}
0 0
- 命令模式(实例二)
- 命令模式(实例一)
- 命令模式(实例五)
- make命令简单实例(二)
- 命令模式(实例三,undo)
- 命令模式(实例四 计算器)
- Android MVC模式实例(二)
- 命令模式(二)
- 设计模式-行为型模式总结实例(二)
- 设计模式-结构型模式总结实例(二)
- 深入浅出外观模式(二):外观模式应用实例
- 设计模式-命令模式(二)
- Java设计模式之二十四(命令模式)
- (二十)Java设计模式之命令模式
- 命令模式删除oracle实例
- C#命令模式实例代码
- java 操作Redis string 命令 实例(Jedis) (二)
- 设计模式之命令(C++实例代码)
- 6 个强大的 HTML5 应用在线开发工具
- use mongoose to update documents with model and Schema
- Hibernate----@Annotation----一对多----级联
- Shell学习总结
- labview和c#的string到字节数组的转换
- 命令模式(实例二)
- gdb dump binary memory to file
- Android环境搭建以及HelloWorld程序
- 春运首日38万人乘火车离京 有人提前12小时候车
- ASP.NET读取EXCEL文件的三种经典方法
- meteor 简单 视频教程
- Python的逻辑运算
- poj2965
- 未能正确加载包“Microsoft.Data.Entity.Design.Package.MicrosoftDataEntityDesignPackage