Design Mode note Command mode Command

Source: Internet
Author: User

Design Mode note Command mode Command

 

// Conmmand command mode-object behavior mode

 

/*

1: Intention:

Encapsulate a request as an object so that you can parameterize the customer with different requests; queue requests or record request logs,

And supports unrecoverable operations.

2: alias:

Action, Transaction (Transaction)

3: motivation:

4: Applicability:

1> abstract the action to be executed to parameterize an object. Callback functions can be used to express this parameterization mechanism.

That is to say, the Conmmand mode is an object-oriented substitute for the callback mechanism.

2> specify, arrange, and execute requests at different times. A Conmmand object can have a lifetime unrelated to the initial request.

If the request recipient can be expressed in a way unrelated to the address space, the command object responsible for the request can be

Send the request to another process and implement it there.

3> cancelling is supported. After the operation is executed, you can call an interface to cancel the operation.

4> log modification is supported, so that these modifications can be redone when the system crashes.

5> construct a system using high-level operations built on primitive operations.

5: structure:

Client Invoker -------> Command:

| Execute ()

|

| ConcreteCommand:

| -------> Cycler: <------------- Cycler

| Action () Execute ()

{Cycler-> Action ();}

|---> State

6: participants:

1> Command:

Declares the operation execution interface.

2> ConcreteCommand

1) bind a receiver object to an action.

2) 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> worker er

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

7: collaboration:

1> the Client creates a ConcreteCommand object and specifies its Receiver object.

2> an Invoker object stores the ConcreteCommand object.

3> the Invoker submits a request by calling the Execute Operation of the Conmmand object. If this command can be undone,

The ConcreteCommand stores the current status before executing the Execute Operation to cancel the command.

4> the ConcreteCommand object calls some operations of its Receiver to execute the request.

8: effect:

1> the Command mode decouples the object that calls the operation from the object that guides how to implement the operation.

2> Command is a class object. They can be operated and expanded like other objects.

3> You can assemble multiple commands into a composite command.

4> it is easy to add a new Command because it does not need to change the existing class.

9: Implementation:

1> How intelligent a command object should be:

The capabilities of command objects can be large or small. One extreme is to determine only the receiver and the action to execute the request. The other extreme is yourself.

To implement all functions, no additional recipient objects are required. (This degrades to the policy mode)

2> cancelling and redoing are supported.

If the Command provides a way to reverse the execution of their operations (such as Unexecute or Undo operations), you can cancel and redo the operation.

Function. For this purpose, the ConcreteCommand class needs to store additional status information:

1) The recipient object that actually processes the request.

2) parameters for the receiver to perform the operation.

3) if the operation to process the request changes some values in the recipient object, these values must be stored first. Recipient

You must also provide some operations so that the command can restore the recipient to its previous state.

3> avoid errors during the cancellation process

Use the mento mode to allow the Command to access information without exposing the internal information of other objects.

4> use the C ++ Template

You can use the C ++ template to execute commands that cannot be canceled and do not require parameters. This avoids

Create a Command subclass.

10: Sample Code :*/

 

// Abstract Command class

Class Command

{

Public:

Virtual ~ Command ();

Virtual void Execute () = 0;

Protected:

Command ();

};

 

// ConcreteCommand:

Class OpenCommand: public Command

{

Public:

OpenCommand (Application *);

Virtual void Execute ();

 

Protected:

Virtual const char * AskUser ();

Private:

Application * _ application;

Char * _ response;

};

 

 

OpenCommand: OpenCommand (Application *)

{

_ Application =;

}

 

// First request a name, then add the file, and then open it

Void OpenCommand: Execute ()

{

Const char * name = AskUser ();

If (name! = 0)

{

Document * document = new Document (name );

_ Application-> Add (document );

Document-> Open ();

}

}

 

// ConcreteCommand:

Class PasteCommand: public Command

{

Public:

PasteCommand (Document *);

Virtual void Execute ();

 

Private:

Document * _ document;

};

 

PasteCommand: PasteCommand (Document * doc)

{

_ Document = doc;

}

 

Void PasteCommand: Execute ()

{

_ Document-> Paste ();

}

 

// ConcreteCommand: this command is implemented by a template to avoid unnecessary child classes.

Template

Class SimpleCommand: public Command

{

Public:

// Create an alias: This is a function pointer. If a parameter is null, the system returns a member function pointer of the empty javaser class.

// This command does not require parameters, so it can be declared as a template class.

Typedef void (Receiver: * Action )();

SimpleCommand (Explorer * r, Action a): _ Explorer (r), _ action (){}

Virtual void Execute ();

 

Private:

Action _ action;

Aggreger * _ aggreger;

};

 

Template

Void SimpleCommand : Execute ()

{

(_ Cycler-> * _ action )();

}

 

MyClass * Receiver = new MyClass;

//...

Command * aCommand = new SimpleCommand (Aggreger, & MyClass: Action );

//...

ACommand-> Execute ();

 

// ConcreteCommand: A composite command that executes multiple commands at a time

Class MarroCommand: public Command

{

Public:

MarroCommand ();

Virtual ~ MarroCommand ();

 

Virtual void Add (Command *);

Virtual void Remove (Command *);

 

Virtual void Execute ();

 

Private:

List * _ Cmds;

};

 

Void MarroCommand: Execute ()

{

ListIterator I (_ cmds );

For (I. First ();! I. IsDone (); I. Next ())

{

Command * c = I. CurrentItem ();

C-> Execute ();

}

}

 

Void MarroCommand: Add (Command * c)

{

_ Cmds-> Append (c );

}

 

Void MarroCommand: Remove (Command * c)

{

_ Cmds-> Remove (c );

}

 

 

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.