Php design mode Command (Command mode ).? Php *** command mode ** encapsulates a request as an object so that you can parameterize the customer with different requests, exclude the request or record the request log, and support cancelable /**
* Command mode
*
* Encapsulate a request as an object so that you can parameterize the customer with different requests, exclude requests or record request logs, and support cancelable operations
*/
Interface Command
{
Public function execute ();
}
Class Invoker
{
Private $ _ command = array ();
Public function setCommand ($ command ){
$ This-> _ command [] = $ command;
}
Public function executeCommand ()
{
Foreach ($ this-> _ command as $ command)
{
$ Command-> execute ();
}
}
Public function removeCommand ($ command)
{
$ Key = array_search ($ command, $ this-> _ command );
If ($ key! = False)
{
Unset ($ this-> _ command [$ key]);
}
}
}
Class cycler
{
Private $ _ name = null;
Public function _ construct ($ name ){
$ This-> _ name = $ name;
}
Public function action ()
{
Echo $ this-> _ name. "action
";
}
Public function action1 ()
{
Echo $ this-> _ name. "action1
";
}
}
Class ConcreteCommand implements Command
{
Private $ _ Explorer;
Public function _ construct ($ javaser)
{
$ This-> _ cycler = $ cycler;
}
Public function execute ()
{
$ This-> _ er-> action ();
}
}
Class ConcreteCommand1 implements Command
{
Private $ _ Explorer;
Public function _ construct ($ javaser)
{
$ This-> _ cycler = $ cycler;
}
Public function execute ()
{
$ This-> _ er-> action1 ();
}
}
Class ConcreteCommand2 implements Command
{
Private $ _ Explorer;
Public function _ construct ($ javaser)
{
$ This-> _ cycler = $ cycler;
}
Public function execute ()
{
$ This-> _ er-> action ();
$ This-> _ er-> action1 ();
}
}
$ ObjRecevier = new Receiver er ("No.1 ");
$ ObjRecevier1 = new Receiver er ("No. 2 ");
$ ObjRecevier2 = new Receiver er ("No. 3 ");
$ ObjCommand = new ConcreteCommand ($ objRecevier );
$ ObjCommand1 = new ConcreteCommand1 ($ objRecevier );
$ ObjCommand2 = new ConcreteCommand ($ objRecevier1 );
$ ObjCommand3 = new ConcreteCommand1 ($ objRecevier1 );
$ ObjCommand4 = new ConcreteCommand2 ($ objRecevier2); // use two methods of Recevier
$ ObjInvoker = new Invoker ();
$ ObjInvoker-> setCommand ($ objCommand );
$ ObjInvoker-> setCommand ($ objCommand1 );
$ ObjInvoker-> executeCommand ();
$ ObjInvoker-> removeCommand ($ objCommand1 );
$ ObjInvoker-> executeCommand ();
$ ObjInvoker-> setCommand ($ objCommand2 );
$ ObjInvoker-> setCommand ($ objCommand3 );
$ ObjInvoker-> setCommand ($ objCommand4 );
$ ObjInvoker-> executeCommand ();
[/Code]
Http://www.bkjia.com/PHPjc/323675.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323675.htmlTechArticle? Php/*** command mode ** encapsulates a request as an object so that you can parameterize the customer with different requests, exclude the request or record the request log, and support cancelable...