Java design Mode _ (Behavioral) _ Command mode __java

Source: Internet
Author: User

Quote Encyclopedia

In the software system, the "behavior requester" and "the performer" usually present a "tight coupling". But in some cases, such as "record, Undo/Redo, transaction" and so on behavior, this kind of cannot resist the change the tight coupling is not suitable. In this case, how to decouple the "behavior requester" from the "act-creator". Abstract a set of behaviors into objects to achieve loose coupling between the two. That's the command pattern.


Pattern Description

1. The essence of the command mode is to encapsulate the order, separating the responsibility for issuing the order and the responsibility for executing the order.
2. Each command is an operation: the requesting party makes a request to perform an action, the receiving party receives the request, and performs the action.
3. The command mode allows the requesting party to be independent of the receiving party so that the requesting party does not have 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.
4. The command mode makes the request itself an object that can be stored and delivered as well as other objects.
5. The key to command mode is the introduction of the abstract command interface, and the sender for the abstract Command interface programming, only the implementation of the abstract command interface to the specific command to be associated with the receiver.


Related Roles

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.
A specific command (Concretecommand) role: Defines a weak coupling between a receiver and a behavior; implements the Execute () method, which is responsible for invoking the corresponding action of the receiver. The Execute () method is often called an execution 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.



Specific implementation:




Specific code:

1. Abstract command role

Abstract command role Public
interface Command {
	//Commands Execute processing method public
	void Execute (String msg);


2, the specific command role class

The specific command role class public class
Realcommand implements command {
	//recipient
	private Receiver Receiver;

	Public Realcommand (Receiver Receiver) {
		this.receiver = Receiver;
	}

	@Override public
	void execute (String msg) {
		this.receiver.action (msg);
	}
}

3. Receive Role Class

Receive role class public
class Receiver {
	/**
	 * @Description: Execute command Operation
	 * @param msg/public
	void Action (String msg) {
		System.out.println ("Receive:" + msg);
		SYSTEM.OUT.PRINTLN ("Command execution Processing!");
	}


4. Request Role Class

Request role class public
class Invoker {
	//Command object
	Private command command;

	Public invoker (Command command) {
		this.command = command;
	}

	Request action method public
	void Handler (String msg) {
		this.command.execute (msg);
	}
}


5. Client-side test

public class Client {public

	static void Main (string[] args) {
		//Create recipient
		Receiver Receiver = new Receiver (); 
  //the command command
		= new Realcommand (receiver);
		Create the sender requestor
		invoker Invok = new invoker (command);
		Execute request operation
		String msg = "./start.sh";
		Invok.handler (msg);
	}


Through the above simple code to achieve the command mode, output:

Receive:./start.sh
Command execution Processing!


Command mode applicable scenario:

1. The system needs to decouple the requesting caller and the request receiver so that the caller and receiver do not interact directly. 2. The system needs to specify the request at different times, queue the request, and execute the request. 3. The system needs to support command revocation (undo) operations and Recovery (REDO) operations. 4. The system needs to combine a set of actions to support macro commands.
pros and Cons:

Advantages:
1. Reduce the coupling between objects.
2. New commands can be easily added to the system.
3. A combination command can be designed more easily.
4. Invoke the same method to implement different functions.

Disadvantages:
Using command mode may cause some systems to have too many specific command classes. Because you need to design a specific command class for each command, some systems may require a large number of specific command classes, which will affect the use of command patterns.






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.