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


    1 <?php
    2 /**
    3 * 命令模式
    4 *
    5 * 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
    6 */
    7 interface Command
    8 {
    9 publicfunction execute();
    10 }
    11
    12 class Invoker
    13 {
    14 private$_command=array();
    15 publicfunction setCommand($command) {
    16 $this->_command[] =$command;
    17 }
    18
    19 publicfunction executeCommand()
    20 {
    21 foreach($this->_command as$command)
    22 {
    23 $command->execute();
    24 }
    25 }
    26
    27 publicfunction 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 publicfunction __construct($name) {
    42 $this->_name =$name;
    43 }
    44
    45 publicfunction action()
    46 {
    47 echo$this->_name." action<br/>";
    48 }
    49
    50 publicfunction action1()
    51 {
    52 echo$this->_name." action1<br/>";
    53 }
    54 }
    55
    56 class ConcreteCommand implements Command
    57 {
    58 private$_receiver;
    59 publicfunction __construct($receiver)
    60 {
    61 $this->_receiver =$receiver;
    62 }
    63
    64 publicfunction execute()
    65 {
    66 $this->_receiver->action();
    67 }
    68 }
    69
    70 class ConcreteCommand1 implements Command
    71 {
    72 private$_receiver;
    73 publicfunction __construct($receiver)
    74 {
    75 $this->_receiver =$receiver;
    76 }
    77
    78 publicfunction execute()
    79 {
    80 $this->_receiver->action1();
    81 }
    82 }
    83
    84 class ConcreteCommand2 implements Command
    85 {
    86 private$_receiver;
    87 publicfunction __construct($receiver)
    88 {
    89 $this->_receiver =$receiver;
    90 }
    91
    92 publicfunction 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();
  • 相关阅读:
    IDEA插件之 CodeGlance(无需滚动的代码地图)
    【翻译】面向自然语言处理的深度学习(一)
    如何估算神经网络参数占用多少内存或显存容量
    Latex向上向下取整语法 及卷积特征图高宽计算公式编辑
    自动测试LeetCode用例方法
    C# Wpf 文件保存对话框
    YOLO实践初探
    前中后序递归遍历树的体会 with Python
    Python 中日期函数
    Tensorflow Windows安装
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2082252.html
Copyright © 2020-2023  润新知