Commands (command) mode
The command mode belongs to the behavior pattern "GOF95" of the object. Command mode is also known as action mode or transaction (Transaction) mode. The command mode encapsulates a request or operation into an object. The command mode allows the system to parameterize clients using different requests, queuing up requests, or logging request logs, which provides the ability to undo and restore commands.
The command mode is the encapsulation of the command. The command mode divides the responsibility for issuing the command from the responsibility of executing the command and delegating it to different objects.
Each command is an action: the requesting party makes a request to perform an action, the receiving party receives the request, and executes the action. The command mode allows the requesting party to be independent of the receiving party, making it unnecessary for the requesting party to know the interface of the party receiving the request, not to mention how the request was received, and whether the operation was executed, when it was executed, and how it was executed.
II. structure of the command pattern
The class diagram for the command mode is as follows:
The command mode involves five roles, each of which is:
Client role: Creates a specific command (Concretecommand) object and determines its recipient.
Command role: Declares an abstract interface to all specific command classes. This is an abstract role.
The specific command (Concretecommand) role: Defines a weak coupling between the receiver and the behavior, implements the Execute () method, and is responsible for invoking the corresponding operation to receive the test. The Execute () method is often called a practice method.
Requester (Invoker) role: is responsible for invoking the Command object execution request, the related method is called the action method.
Recipient (Receiver) role: be responsible for implementing and executing a request specifically. Any class can be a receiver, and the method of implementing and executing the request is called the action method.