Technology Sharing

Discuss the command pattern and its application

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Command Mode

Command ModeIt is a behavioral design pattern that transforms a request into a separate object that contains all the information related to the request. This transformation allows you to parameterize methods based on different requests, delay or queue request execution, and implement undoable operations.

Command Mode Structure

insert image description here

  1. senderThe Sender class, also known as the Invoker class, is responsible for initializing the request and must contain a member variable to store a reference to the command object. The Sender triggers the command without sending a request directly to the Receiver. Note that the Sender is not responsible for creating the command object: it usually receives a pre-generated command from the client via the constructor.
  2. OrderThe (Command) interface usually declares only one method that executes the command.
  3. Specific commands Concrete Commands implement various types of requests. Concrete commands do not do the work themselves, but delegate the call to a business logic object. However, to simplify the code, these classes can be merged. The parameters required by the receiving object to execute the method can be declared as member variables of the concrete command. You can make the command object immutable and only allow these member variables to be initialized through the constructor.
  4. ReceiverThe (Receiver) class contains some of the business logic. Almost any object can be a receiver. Most commands only handle the details of how to pass the request to the receiver, and the receiver itself will do the actual work.
  5. Client(Client) creates and configures a specific command object. The client must pass all request parameters, including the recipient entity, to the command's constructor. After that, the generated command can be associated with one or more senders.

Command mode general code:

//抽象接收者
public abstract class Receiver{
	public abstract void operation();
}

//具体接收者
public class Recevier1 extends Recevier{
	public void operation(){
		...
	}
}

//通用命令接口
public interface Command{
	void execute();
}

//具体命令类
public class ConcreteCommand1 implements Command{
	private Receiver receiver;
	
	public ConcreteCommand1(Receiver _receiver){
		 this.receiver = _receiver;
	}
	
	public void execute(){
		this.receiver.operation();
	}
}

//调用者类
public class Invoker{
	private Command command;
	
	public void setCommand(Command _command){
		this.command = _command;
	}
	
	public void executeCommand(){
		this.command.execute();
	}
	
}

//主程序类
public class Client{
	public static void main(String[] args){
		//调用者
		Invoker invoker = new Invoker();
		//接收者
		Receiver receiver1 = new Receiver1();
		//定义一个命令
		Command command = new ConcreteCommand1(receiver1);
		
		invoker.setCommand(command);
		invoker.executeCommand();
	}
}