Java Design mode Command pattern

Source: Internet
Author: User

I. Overview

The command pattern can completely decouple the request sender from the receiver, and there is no direct reference between the sender and the recipient, and the object sending the request needs only 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 mounted in a collection or queue, by traversing the collection, to the batch processing of the command]

3>, when executing a command to trigger the execution of multiple specific commands, thereby implementing batch processing of the command, which is called a combination command or macro command;


Third, UML class diagram


Iv. participants

1>, Command (Abstract command Class): An abstract command class is generally an abstract class or interface in which the Execute () method used to execute the request is declared, through which the related actions of the requesting receiver can be invoked.

2>, Concretecommand (Specific command Class): The specific command class is the subclass of the abstract command class, implements the method declared in the abstract command class, which corresponds to the specific command receiver object, which binds the action of the receiver object. When the Execute () method is implemented, the associated operation (operation) of the recipient object is invoked.

3>, Invoker (command initiator/message requestor): Has an association with an abstract command class that injects a specific command object into it at the start of the command and then invokes the execution method (execute) of the specific Command object to indirectly implement the related operation of invoking the request receiver.

4>, Receiver (command/message receiver): The receiver performs the action associated with the request, which implements the business processing of the request (Operation method Execution).

V. Use case Learning

1. Command receiver A:receivera.java

/** * Command recipient A * @author  [email protected] * */public class Receivera {public void operation () {System.out.println ("I am the command to connect Recipient A, I have received the command, is executing the appropriate business operation Method ");}}
2. Command receiver B:receiverb.java

/** * Command Recipient B * @author  [email protected] * */public class Receiverb {public void operation () {System.out.println ("I am the command to connect Recipient B, I have received the command that is executing the appropriate business operation Method ");}}
3. Abstract Command class: Command.java

/** * Command Abstract class * @author  [email protected] * */public abstract class Command {public abstract void execute ();}
4, the specific command class A:concretecommanda.java

/** * Specific Command class a<br/> * associated with the command recipient Receivera * @author  [email protected] * */public class Concretecommanda extends Com Mand {private Receivera receiver;public Concretecommanda () {receiver = new Receivera ();} @Overridepublic void Execute () {//Call the execution method of the specific command receiver Receiver.operation ();}}
5, the specific command class B:concretecommandb.java

/** * Specific Command class b<br/> * associated with the command recipient Receiverb * @author  [email protected] * */public class Concretecommandb extends Com Mand {    private receiverb receiver;public concretecommandb () {receiver = new Receiverb ();} @Overridepublic void Execute () {//Call the execution method of the specific command receiver Receiver.operation ();}}
6. Command Sender/message requestor: Invoker.java

/** * Command/message sender * @author  [email protected] * */public class Invoker {//Maintain a reference to an abstract command class private command command;/** * to specific life A reference to the object * @param command */public void setcommand (Command command) {this.command = command;} /** * Send command <br/> * Invoke specific command execution class, indirectly pass the message/command to the command receiver execution */public void RunCommand () {Command.Execute ()}}
7. Client Test class: Client.java

public class Client {public static void main (string[] args) {Command command = null;/* * If you want the command to be sent to command recipient B (receiverb) processing * As long as the Receiverb-related command class concretecommandb is instantiated, the code can be modified as follows: * command = new concretecommandb (); *  * Following is an instantiation of the command class Concretecommanda and sending the command message to the associated Receivera processing */command = new Concretecommanda (); Invoker Invoker = new Invoker (); invoker.setcommand (command); Invoker.runcommand ();}}

Vi. Other



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.