Java and mode Learning Series-command mode

Source: Internet
Author: User

 

I,Command)Schema Structure

In command mode, a request or operation is encapsulated into an object. The command mode allows the system to parameterize the client using different requests, queue requests or record request logs, and provides command revocation and recovery functions.

Command mode is the encapsulation of commands. The command mode separates the responsibility for issuing commands from the responsibility for executing commands and delegates them to different objects. The command mode is similar to the C language callback. Because Java does not support function pointers, the command mode can achieve the same purpose.

Shows the structure:

The customer role code is as follows:

Public class client {

Public static
Void main (string [] ARGs ){

Extends er extends ER =
New Explorer ();

Command command = new
Concretecommand (aggreger );

Invoker = new
Invoker (command );

Invoker. Action ();

}

}

A specific command is bound to a receiver. In a specific command, the receiver's processing method is called, and client requests can call different command objects. To add a new command, you only need to add a new specific command object and receiver, without modifying the client code, and the client will not notice it.

II, Command mode in JavaApplication in

AWTEvent Processing

The Java language uses the command mode to process the event Delegate processing model of the Java. AWT library. In this event processing model, the command object implements the listener interface of AWT, which is equivalent to the command interface. To connect a command object to an AWT component, register it as an event listener. The component knows the listener interface, but does not care how the interface is implemented. In other words, the AWT component does not care about the implementation operations represented by commands.

III,
Use Cases of command mode

Advantages and disadvantages of command mode

Advantages: (1) command mode makes it easy to add new commands to the system (2) allow the party receiving the request to decide whether to reject the request (3) it is easy to design a command queue (4). It can easily implement the Undo and redo (5) of the request and easily log the command as needed.

Applicable situations

1. Use the command mode as an alternative to "Callback" in the object-oriented system. "Callback" refers to registering a function and then calling it later.

2. You must specify the request at different times and queue the request. A command object and the original request sender can have different lifecycles. At this time, the command receiver can be local or another address on the network.

3. The system must support command revocation. The command object can store the status. When the client needs to cancel the effect of the command, it can call the Undo () method to undo it. The command object can also provide the Redo () method for the client to re-execute the command when needed.

4. If a system wants to update all the data in the system to the log, you can read all the data update commands in the log and call execute () again when the system crashes () to restore the data updates made by the system before the crash.

5. A one-person system must support transactions ). A transaction structure encapsulates a set of data update commands. Using the command mode to implement the transaction structure allows the system to add new transaction types.

IV,
Relationship between command mode and other modes

Command mode and merging mode

The merging mode can be applied to the synthesis of command classes, and macro command classes can be synthesized from several specific command classes. The recorded commands can be re-executed at any time as needed. This is called the macro command set function.

Memorandum Mode

If the command needs to be withdrawn and restored, the memorandum mode can be used to store information about the command's effect status.

V,
SwingDATABASE Command revocation and recovery

Swing provides a systematic solution for Undo and recovery in the javax. Swing. Undo library. Although the javax. Swing. Undo Library is part of the swing library, it can be used with both the swing component and the AWT component.

Every command or operation is called an edit in swing ). Every unrecoverable command and operation is called an unrecoverable edit in swing.

(1) interface undoableedit. To enable the undoableedit interface for a class, you need to implement the undoableedit interface.

(2) abstract class abstractundoableedit is the minimum implementation of the undoableedit interface and is also the base class of other classes in the javax. Swing. Undo library.

(3) The stateeditable interface is a required interface for all classes that can be edited.

(4) The sateedit class represents the status of an editable class.

(5) The undomanager class is responsible for managing all edits to an editable class.

 

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

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.