Design principle:
1. The essence of the command pattern is to encapsulate the command, separating the responsibility for issuing the order and the responsibility for executing the order.
2. Each command is an action: the requesting party makes a request, requests an action, the receiving party receives the request, and executes the action.
3. The command pattern allows the requesting party and the receiving party to be independent, so that the requesting party does not have to know the interface of the party receiving the request, not to know how the request was received, and whether the operation was executed, when it was executed, and how it was executed.
4. The command pattern makes the request itself an object that can be stored and passed in the same way as other objects.
5. The key to the command pattern is the introduction of an abstract command interface, and the sender is programmed for an abstract command interface, and only specific commands that implement an abstract command interface can be associated with the receiver.
?
Invoker is the caller of the command, the command defines the interface, Concretecommand is the implementation of the command virtual, receiver is the command really implemented
The client creates a specific command object and sets the recipient of the Command object.
Typical examples:
The TV is the receiver of the request, the remote control is the sender of the request, there are some buttons on the remote control, different buttons correspond to different operation of the TV. The abstract command role is played by a command interface, with three specific command classes implementing an abstract command interface, three specific command classes representing three operations: turning on the TV, turning off the TV, and switching channels. Obviously, the TV remote is a typical application example of command mode.
design mode (i): Command mode