mvc5 网站开发之美 pdf,重庆市工程建设信息,个体工商注册查询平台,seo推广公司 什么意思定义
命令模式#xff08;Command Pattern#xff09;是一种行为设计模式#xff0c;它将请求封装为一个对象#xff0c;从而使您能够使用不同的请求、排队请求或记录请求#xff0c;并支持可撤销的操作。该模式通过将请求与其执行分离#xff0c;使得请求者和接收者之间…定义
命令模式Command Pattern是一种行为设计模式它将请求封装为一个对象从而使您能够使用不同的请求、排队请求或记录请求并支持可撤销的操作。该模式通过将请求与其执行分离使得请求者和接收者之间的耦合度降低。
UML图 Command命令接口定义一个接口通常包含一个 execute() 方法。ConcreteCommand具体命令实现命令接口定义了与接收者的绑定调用接收者的方法以实现请求。Receiver接收者具体执行请求的类包含执行相关操作的具体方法。Invoker调用者持有命令对象并在需要时调用它。它可以调度请求并可以支持多种命令。Client客户端创建具体命令对象并将其与接收者绑定同时将命令对象传递给调用者。
代码
// Command interface
interface Command {void execute();
}// Receiver
class Light {public void turnOn() {System.out.println(Light is ON);}public void turnOff() {System.out.println(Light is OFF);}
}// ConcreteCommand for turning on the light
class TurnOnLightCommand implements Command {private Light light;public TurnOnLightCommand(Light light) {this.light light;}Overridepublic void execute() {light.turnOn();}
}// ConcreteCommand for turning off the light
class TurnOffLightCommand implements Command {private Light light;public TurnOffLightCommand(Light light) {this.light light;}Overridepublic void execute() {light.turnOff();}
}// Invoker
class RemoteControl {private Command command;public void setCommand(Command command) {this.command command;}public void pressButton() {command.execute();}
}// Client code
public class CommandPatternDemo {public static void main(String[] args) {Light light new Light();Command turnOn new TurnOnLightCommand(light);Command turnOff new TurnOffLightCommand(light);RemoteControl remote new RemoteControl();remote.setCommand(turnOn);remote.pressButton(); // Output: Light is ONremote.setCommand(turnOff);remote.pressButton(); // Output: Light is OFF}
}
优点
解耦请求者与接收者之间的解耦使得系统更加灵活。可扩展性可以轻松添加新的命令而不修改现有代码。支持撤销可以实现命令的撤销和重做功能。
缺点
命令对象数量多如果命令数量较多可能会增加系统的复杂性。实现复杂性对于复杂的命令和接收者可能需要更多的设计工作。
使用场景
当需要将请求调用的对象和请求的执行对象解耦时。当需要支持撤销、重做操作时。当需要对请求进行日志记录或排队时。当需要实现事务处理时。