• php设计模式 Command(命令模式)


    简介:这是php设计模式 Command(命令模式)的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

    class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=338753' scrolling='no'>
    1 <?php
    2 /**
    3 * 命令模式
    4 *
    5 * 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
    6 */
    7 interface Command
    8 {
    9 public function execute();
    10 }
    11
    12 class Invoker
    13 {
    14 private $_command = array();
    15 public function setCommand($command) {
    16 $this->_command[] = $command;
    17 }
    18
    19 public function executeCommand()
    20 {
    21 foreach($this->_command as $command)
    22 {
    23 $command->execute();
    24 }
    25 }
    26
    27 public function removeCommand($command)
    28 {
    29 $key = array_search($command, $this->_command);
    30 if($key !== false)
    31 {
    32 unset($this->_command[$key]);
    33 }
    34 }
    35 }
    36
    37 class Receiver
    38 {
    39 private $_name = null;
    40
    41 public function __construct($name) {
    42 $this->_name = $name;
    43 }
    44
    45 public function action()
    46 {
    47 echo $this->_name." action<br/>";
    48 }
    49
    50 public function action1()
    51 {
    52 echo $this->_name." action1<br/>";
    53 }
    54 }
    55
    56 class ConcreteCommand implements Command
    57 {
    58 private $_receiver;
    59 public function __construct($receiver)
    60 {
    61 $this->_receiver = $receiver;
    62 }
    63
    64 public function execute()
    65 {
    66 $this->_receiver->action();
    67 }
    68 }
    69
    70 class ConcreteCommand1 implements Command
    71 {
    72 private $_receiver;
    73 public function __construct($receiver)
    74 {
    75 $this->_receiver = $receiver;
    76 }
    77
    78 public function execute()
    79 {
    80 $this->_receiver->action1();
    81 }
    82 }
    83
    84 class ConcreteCommand2 implements Command
    85 {
    86 private $_receiver;
    87 public function __construct($receiver)
    88 {
    89 $this->_receiver = $receiver;
    90 }
    91
    92 public function execute()
    93 {
    94 $this->_receiver->action();
    95 $this->_receiver->action1();
    96 }
    97 }
    98
    99
    100 $objRecevier = new Receiver("No.1");
    101 $objRecevier1 = new Receiver("No.2");
    102 $objRecevier2 = new Receiver("No.3");
    103
    104 $objCommand = new ConcreteCommand($objRecevier);
    105 $objCommand1 = new ConcreteCommand1($objRecevier);
    106 $objCommand2 = new ConcreteCommand($objRecevier1);
    107 $objCommand3 = new ConcreteCommand1($objRecevier1);
    108 $objCommand4 = new ConcreteCommand2($objRecevier2); // 使用 Recevier的两个方法
    109
    110 $objInvoker = new Invoker();
    111 $objInvoker->setCommand($objCommand);
    112 $objInvoker->setCommand($objCommand1);
    113 $objInvoker->executeCommand();
    114 $objInvoker->removeCommand($objCommand1);
    115 $objInvoker->executeCommand();
    116
    117 $objInvoker->setCommand($objCommand2);
    118 $objInvoker->setCommand($objCommand3);
    119 $objInvoker->setCommand($objCommand4);
    120 $objInvoker->executeCommand();

    爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

    http://biancheng.dnbcw.info/php/338753.html pageNo:9
  • 相关阅读:
    小技巧
    常用的交互设计软件
    Android studio 使用SVN需要忽略的文件
    android studio 使用SVN 锁定文件,防止别人修改(基于Android studio 1.4 )
    git 和 github 关系?
    Double 数据保留两位小数一:五舍六入
    设计模式
    Java中关于日期类那些方法
    ios 开源免费接口
    华为招聘机试整理5:简单四则运算
  • 原文地址:https://www.cnblogs.com/ooooo/p/2246976.html
Copyright © 2020-2023  润新知