zoukankan      html  css  js  c++  java
  • PHP的设计模式总结命令链模式

    命令链 模式以松散耦合主题为基础,发送消息、命令和请求,或通过一组处理程序发送任意内容。每个处理程序都会自行判断自己能否处理请求。如果可以,该请求被处理,进程停止。您可以为系统添加或移除处理程序,而不影响其他处理程序。

     1 <?php   
     2 interface icommand{
     3     function oncommand($name,$arg);
     4 };
     5 
     6 class commandchain{
     7     private $_commands=array();
     8     public function addCommand($cmd){
     9         $this->_commands[]=$cmd;
    10     }
    11     public function runCommand($name,$args)
    12     {
    13         foreach ($this->_commands as $cmd)
    14         {$cmd->oncommand($name,$args);}
    15     }
    16 }
    17 
    18 class mycommand implements icommand{
    19     function oncommand($name,$arg){
    20            if($name=='mytask'){
    21                echo 'it is my task'.'参数是'.$arg;
    22            }
    23         }
    24     }
    25     
    26 class yourcommand implements icommand{
    27         function oncommand($name,$arg){
    28             if($name=='yourtask'){
    29                 echo 'it is your task'.'参数是'.$arg;
    30             }
    31         }
    32     }
    33     
    34 $mycom=new mycommand();
    35 $yourcom=new yourcommand();
    36 $comchain=new commandchain();
    37 $comchain->addCommand($mycom);
    38 $comchain->addCommand($yourcom);
    39 $comchain->runCommand('mytask',1);
    40 $comchain->runCommand('yourtask',6);
    41 
    42 
    43 
    44 ?> 

    代码依旧比较简单。请见谅。

  • 相关阅读:
    关于SuperSocket启动失败
    ffmpeg 常用命令
    Url中有中文参数需要编码解码
    单例模式
    c# 文件夹重命名
    一个既有winform又有webapi 的例子
    数据库查询字段的结构和长度
    Jquery 展开收起
    ajax即时修改
    EFCore 迁移
  • 原文地址:https://www.cnblogs.com/phplover/p/2972160.html
Copyright © 2011-2022 走看看