Php design mode Command (Command mode)

Source: Internet
Author: User
Encapsulate a request as an object so that you can parameterize the customer with different requests, exclude or record the request logs, and support cancelable operations. The code is as follows:
/**
* 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 ();

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.