命令模式:将请求封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
public class Light {
public void on() {
System.out.println("trun on the light");
}
}
public interface Command {
void execute();
}
public class LightOnCommand implements Command{
Light light ;
@Override
public void execute() {
light.on();
}
public LightOnCommand(Light light) {
super();
this.light = light;
}
}
public class SimpleRemoteControl {
Command solt;
public SimpleRemoteControl() {
}
public SimpleRemoteControl(Command solt) {
super();
this.solt = solt;
}
public void buttonWasPressed() {
solt.execute();
}
}