introduction:
这样做的目的可以减少“硬编码”,动态决定命令组合,在使用上更灵活,一般来说,宏命令的做法更优雅,也需要较少的新代码。
demo:
description:模拟开舞会的场景,此时会需要灯光和音乐
Light.java
public class Light {
private String name;
public Light(String name){
this.name = name;
}
public void on(){
System.out.println(name+" on");
}
public void off(){
System.out.println(name+" off");
}
}
public class Player {
private String name;
public Player(String name){
this.name = name;
}
public void off(){
System.out.println(name+" off");
}
public void on(){
System.out.println(name+" on");
}
public void volumn(int num){
System.out.println(name+" : "+num);
}
public void cd(){
System.out.println(name+" cd");
}
public void dvd(){
System.out.println(name+" dvd");
}
}
public interface Command {
void execute();
void undo();
}
public interface Command {
void execute();
void undo();
}
public class LightOnCommand implements Command{
private Light light;
public LightOnCommand(Light light){
this.light = light;
}
@Override
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}
public class NoCommand implements Command{
@Override
public void execute() {
// TODO Auto-generated method stub
}
@Override
public void undo() {
// TODO Auto-generated method stub
}
}
public class PlayerOffCommand implements Command{
private Player player;
public PlayerOffCommand(Player player){
this.player = player;
}
@Override
public void execute() {
player.off();
}
@Override
public void undo() {
player.on();
player.cd();
player.volumn(11);
}
}
public class PlayerOnCommand implements Command{
private Player player;
public PlayerOnCommand(Player player){
this.player = player;
}
@Override
public void execute() {
player.on();
player.cd();
player.volumn(11);
}
@Override
public void undo() {
player.off();
}
}
public class MacroOffCommand implements Command{
private Command[] commands;
public MacroOffCommand(Command[] commands){
this.commands = commands;
}
@Override
public void execute() {
for(int i=0;i
public class MacroOnCommand implements Command{
private Command[] commands;
public MacroOnCommand(Command[] commands){
this.commands = commands;
}
@Override
public void execute() {
for(int i=0;i
public class RemoteControl {
private Command[] onCommands;
private Command[] offCommands;
private Command undoCommand;
public RemoteControl(){
Command noCommand = new NoCommand();
onCommands = new Command[1];
offCommands = new Command[1];
this.undoCommand = noCommand;
for(int i=0;i
public class ControllerLoader {
public static void main(String[] args) {
Light light = new Light("party light");
Player player = new Player("party player");
Command partyLightOn = new LightOnCommand(light);
Command partyLightOff = new LightOffCommand(light);
Command partyPlayerOn = new PlayerOnCommand(player);
Command partyPlayerOff = new PlayerOffCommand(player);
Command[] onCommands = {partyLightOn,partyPlayerOn};
Command[] offCommands = {partyLightOff,partyPlayerOff};
Command macroOn = new MacroOnCommand(onCommands);
Command macroOff = new MacroOffCommand(offCommands);
RemoteControl remoteControl = new RemoteControl();
remoteControl.setCommand(0, macroOn, macroOff);
System.out.println(remoteControl);
remoteControl.onButtonPressed(0);
remoteControl.offButtonPressed(0);
remoteControl.undoButtonPressed();
}
}
运行结果:
--- command begin ---
[0]MacroOnCommand , MacroOffCommand
party light on
party player on
party player cd
party player : 11
party light off
party player off
party light on
party player on
party player cd
party player : 11