I. Overview
The command pattern can completely decouple the request sender from the receiver. There is no direct reference between the sender and the receiver, and the object sending the request only needs to know how to send the request, without having to know how to complete the request. The core is to introduce the command class, through the command class to reduce the coupling between sender and receiver, the request sender only need to specify a command object, and then through the command object to invoke the request receiver's processing method, command mode is an object behavior pattern.
Second, the use of the scene
1>, the system requires the decoupling of the requesting sender and receiver, so that the caller and receiver do not interact directly;
2>, similar message request/Command queue processing. [command is loaded in a collection or queue, by iterating through the collection, reaching the batch processing of the command]
3>, when running a command, triggers the operation of more than one command, thereby implementing batch processing of the command. Such a command is called a combination command or a macro command;
Third, UML class diagram
Iv. Participant
1>, Command (Abstract command Class): An abstract command class is usually an abstract class or interface. Methods such as execute () that are used to run the request are declared in. These methods enable you to invoke the related actions of the requesting receiver.
2>, Concretecommand (Verbose command Class): The verbose command class is a subclass of an abstract command class that implements a method declared in an abstract command class, which corresponds to a detailed command receiver object. Binds the action of the recipient object. When the Execute () method is implemented. The associated operation (operation) that will invoke the recipient object.
3>, Invoker (command initiator/message requestor): Has an association with an abstract command class, injects a verbose command object into it when the command is launched, and then invokes the Run method (execute) of the detailed command object to indirectly implement the related operation of invoking the request receiver.
4>, Receiver (command/message receiver): The receiver runs the action associated with the request. It implements the business processing of the request in detail (the operation method runs).
V. Use case Learning
1. Command receiver A:receivera.java
/** * Command recipient A * @author [email protected] * */public class
Java Design mode Command pattern