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
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.
OrderThe (Command) interface usually declares only one method that executes the command.
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.
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.
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.