Design Mode-behavior-Command)

Source: Internet
Author: User

Overview
Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.
Applicability
Abstract The Action to be executed to parameterize an object. You can use the callback (c a l B a c k) function in the process language to express this parameterization mechanism. A callback function is a function that is registered at a certain place and will be called at a later time. The C o m a n d mode is an object-oriented alternative to the callback mechanism.
Specify, arrange, and execute requests at different times. A C o m a n d object can have a lifetime unrelated to the initial request. If the recipient of a request can be expressed in a way unrelated to the address space, the command object responsible for the request can be sent to another different process and implemented there.
Cancel the operation. The C o m a n d E x c u t e operation can store the status before performing the operation. This status is used to eliminate the impact of the operation when the operation is canceled. The C o m a n d interface must add a U n e x e c u t e operation, which cancels the effect of the last E x e c u t e call. The executed command is stored in a history list. You can traverse this list backward and forward and call U n e x e c u t e and E x e c u t e E e and redo ".
Logs can be modified so that the modifications can be redone when the system crashes. Add the loading and storage operations to the C o m a n d interface to maintain a consistent change log. Recovery from a crash involves re-reading the recorded commands from the disk and re-executing them with the E x e c u t e operation.
Construct a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (t r a n s a c t I o n. A transaction encapsulates a group of changes to the data. The C o m a n d mode provides a method for modeling transactions. C o m a n d has a common interface that allows you to call all transactions in the same way. At the same time, it is easy to add new transactions to expand the system.
Structure

Participants
1. Command
Declares the operation execution interface.

2. ConcreteCommand
Binds a recipient object to an action.
Call the corresponding operation of the receiver to Execute.

3. Client
Create a specific command object and set its receiver.

4. Invoker
This command is required to execute this request.

5. Cycler
Know how to perform operations related to executing a request. Any class can be used as a receiver.

Example
Package com. sql9.actioned;
 
Class extends er {
Public void request (){
System. out. println ("handler er here does request .");
}
 
}
 
Abstract class Command {
 
Protected extends er;
 
Public Command (extends er extends ER ){
This. Cycler = Cycler;
}
 
Public abstract void execute ();
}
 
Class ConcreteCommand extends Command {
 
Public ConcreteCommand (extends er extends ER ){
Super (Explorer );
}
 
@ Override
Public void execute (){
Receiver. request ();
System. out. println ("command executed in ConcreteCommand ");
// You can do something none of this
}
}
 
Class AnotherCommand extends Command {
 
Public AnotherCommand (extends er extends ER ){
Super (Explorer );
}

Private void doSomeOtherWork (){
System. out. println ("Do some other work in AnotherCommand ");
}

@ Override
Public void execute (){
// Ignore Explorer
DoSomeOtherWork ();
}

}
 
Class Invoker {
 
Private Command command;
 
Public void setCommand (Command command ){
This. command = command;
}
 
Public void execute (){
Command.exe cute ();
}
}
 
Public class CommandTest {
 
Public static void main (String [] args ){
Worker er worker ER = new worker Er ();
Invoker invoker = new Invoker ();
Invoker. setCommand (new ConcreteCommand (caller ));
Invoker.exe cute (); www.2cto.com
Invoker. setCommand (new AnotherCommand (caller ));
Invoker.exe cute ();
}
 
}

Result
Else er here does request.
Command executed in ConcreteCommand
Do some other work in AnotherCommand

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.