Java design mode command pattern and function in Java

Source: Internet
Author: User

The command pattern belongs to the behavior pattern of the object. Command mode is also known as action mode or trading (Transaction) mode.

The command pattern encapsulates a request or operation into an object. The command mode allows the system to parameterize the client using different requests, queue requests, or log request logs, which can provide undo and redo functions for the command.

Structure of the command pattern

  The command pattern is the encapsulation of the command. The command mode divides the responsibility for issuing the command and the responsibility for executing the command, delegating it to different objects.

Each command is an action: the requesting party makes a request to perform an action, the receiving party receives the request, and executes the action. 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.

The command allows the requesting party and the party receiving the request to evolve independently, with the following advantages:

(1) The command mode makes it easy to add new commands to the system.

(2) Allow the party receiving the request to decide whether to veto the request.

(3) It is easier to design a command queue.

(4) The revocation and recovery of the request can be easily achieved.

(5) If required, it is easier to log commands into the logs.

Advantages of the Command mode

more loosely coupled

  The command pattern completely decouples the object that originated the command-the client, and the object that implements the command-that is, the object that initiated the command is completely unaware of who the implementation object is and how it is implemented.

more dynamic Control

  The command mode encapsulates the request, and it can be parameterized, queued and journaled dynamically, which makes the system more flexible.

It's a natural compound command.

Command objects in command mode can be easily combined into composite commands, which are macro commands, making the system easier to operate and more powerful.

Better Extensibility

Since the object that initiated the command is fully decoupled from the specific implementation, it is easy to extend the new command, just implement the new command object, then set the specific implementation object to the Command object when assembling, and then you can use the command object, and the existing implementation does not have to change at all.

Original link http://www.cnblogs.com/java-my-life/archive/2012/06/01/2526972.html

Java design mode command pattern and function in Java

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.