Remember that the best designed pattern is the article "Chasing MM and Design Mode" on the 9CBS.
Here is a description of the Command mode:
Command - I have a MM home to manage very strict, I can't meet, I have to use her younger brother to transfer information between us, what is the instruction to me, write a note to give me her brother. This is not, her brother passed another Command. In order to thank him, I invited him to eat a bowl of soy sauce, and where he said: "I will send my sister three boyfriend to Command, as you have the minimum gas, Please eat face. "
Command mode: Command mode encapsulates a request or operation into an object. Command mode The responsibility of the issued command and the responsibility of executing commands are separated, and delegate different objects. The command pattern allows the request to be opened independently, so that one of the requests does not have to know the interface to receive the request, it is not necessary to know how the request is received, and when is executed, when is executed, how to be executed of. The system supports the undo.
This is the UML map of the Command mode
Pre {Font-Family: "Courier New", Courier, Arial; font-size: 12px;}
. galan {color: # 000000;
. readyword {color: # 993300;
.IDENTIFIER {color: # 000087;
.properties {color: # 000087;
{Color: # 000087;
.LINECMMMENT, .BLOCKCOMMENT {Color: # 808080;
. String {color: # 0000ff;}
Class Command
{
Private function command ()
{
}
//Methods
Public function execute (): void
{
}
Public function unExecute (): void
{
}
}
// "ConcreteCommand"
Class CalculatorCommand Extends Command
{
// fields
PRIVATE VAR $ OPERATOR: STRING;
Private var over;
Private var Calculator: Calculator;
// conncture
Public Function CalculatorCommand (Calculator: Calculator, $ OPERATOR: STRING, OPERAND: NUMBER)
{
THIS.CALCULATOR = CALCULATOR;
THIS. $ operator = $ OPERATOR;
THIS.OPERAND = OPERAND;
}
// Properties
Public Function Set Operator (Value: string): Void
{
$ Operator = Value;
}
Public Function Set Operand (Value: Number): Void
{
Operand = Value;
}
//Methods
Public function execute (): void
{
Calculator.operation ($ OPERATOR, OPERAND);
}
Public function unExecute (): void
{
Calculator.operation (Undo ($ Operator), Operand;
}
// private helper function
Private function undo ($ operator: string): String {
Var undo: string = ""
Switch ($ OPERATOR)
{
Case " ":
Undo = "-";
Break;
Case "-":
Undo = " ";
Break;
Case "*":
Undo = "/";
Break;
Case "/":
Undo = "*";
Break;
}
Return Undo;
}
}
// "Receiver"
Class Calculator
{
// fields
Private var total: Number = 0;
//Methods
Public Function Operation ($ OPERATOR: STRING, OPERAND: NUMBER): VoID
{
Switch ($ OPERATOR)
{
Case " ":
Total = Operand;
Break;
Case "-":
Total - = OPERAND;
Break;
Case "*":
Total * = OPERAND;
Break;
Case "/":
Total / = OPERAND;
Break;
}
Trace ("Total =" TOTAL "(FOLLOWING" $ Operator " Operand ") ")
}
}
// "Invoker"
Class User
{
// fields
Private var Calculator: Calculator;
Private var commands: array = new arch;
Private var current: Number = 0;
Public Function user ()
{
Calculator = new Calculator ();
}
//Methods
Public Function Redo (Levels: Number): Void
{
// Trace (Commands [Current ] InstanceOf Command;
Trace ("---- redo" levels "levels");
// Perform Redo Operations
For (var i = 0; i { Current { Commands [CURRENT ]. Execute (); } } } Public Function Undo (Levels: Number): Void { Trace ("---- undo" levels "levels"); // perform undo operations For (var i = 0; i { IF (Current> 0) { Commands [- current] .unexecute (); } } } Public Function Compute ($ OPERATOR: STRING, OPERAND: VOID { // Create Command Operation and Execute IT Var Command: Command = New CalculatorCommand (Calculator, $ Operator, Operand); Command.execute (); // Add command to undo list Commands.push (Command); CURRENT ; } } Client.fla // Create User And Let Her Compute Var User: user = new user (); User.compute (' ', 100); User.compute ('-', 50); User.compute ('*', 10); User.compute ('/', 2); // undo and then redo some commists User.undo (4); User.Redo (3); Output: Total = 100 (FOLLOWING 100) Total = 50 (FOLLOWING- 50) Total = 500 (FOLLOWING * 10) Total = 250 (FOLLOWING / 2) ---- undo4 levels total = 500 (FOLLOWING * 2) Total = 50 (FOLLOWING / 10) Total = 100 (FOLLOWING 50) Total = 0 (FOLLOWING- 100) ---- Redo 3 Levels Total = 100 (FOLLOWING 100) Total = 50 (FOLLOWING- 50) Total = 500 (FOLLOWING * 10)