Command Design Pattern

Command Design Pattern: Encapsulates requests as objects, which allows you to parameterize other objects with different requests, queues, or log requests. Command mode can also support undo operations.

Use the command pattern when you need to decouple the object making the request from the object executing the request.


Code:
// request caller
public class CommandMain {
	Command command;

	public void setCommand(Command command) {
		this.command = command;
	}
	
	public void execute(){
		command.execute();
	}
}
//command object
public class LightCommand implements Command{
	private Light light;
	public LightCommand(Light light){
		this.light = light;
	}
	@Override
	public void execute() {
		light.on();
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326697438&siteId=291194637