Java Design mode Command pattern

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.