zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    494. Target Sum 添加标点符号求和
    636. Exclusive Time of Functions 进程的执行时间
    714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票
    377. Combination Sum IV 返回符合目标和的组数
    325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
    275. H-Index II 递增排序后的论文引用量
    274. H-Index论文引用量
    RabbitMQ学习之HelloWorld(1)
    java之struts2的数据处理
    java之struts2的action的创建方式
  • 原文地址:https://www.cnblogs.com/ooooo/p/2246976.html
Copyright © 2011-2022 走看看