zoukankan      html  css  js  c++  java
  • php 23种设计模式

    命令模式

    将一个请求封装为一个对象,从而使用户可用不同的请求对客户进行参数化。对请求排队或记录请求日志,以及支持撤销的操作。

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

    命令模式的四种角色:

    1. 接收者(Receiver)负责执行与请求相关的操作

    2. 命令接口(Command)封装execute()、undo()等方法

    3. 具体命令(ConcreteCommand)实现命令接口中的方法

    4. 请求者(Invoker)包含Command接口变量

    interface ICommand {
        function onCommand($name, $args);
    }
    
    class CommandChain {
        private $_commands = array();
        public function addCommand($cmd) {
            $this->_commands []= $cmd;
        }
    
        public function runCommand($name, $args) {
            foreach($this->_commands as $cmd) {
                if ($cmd->onCommand($name, $args)) return;
            }
        }
    }
    
    class UserCommand implements ICommand {
        public function onCommand($name, $args) {
            if ($name != 'addUser') return false;
            echo("UserCommand handling 'addUser'
    ");
            return true;
        }
    }
    
    class MailCommand implements ICommand {
        public function onCommand($name, $args) {
            if ($name != 'mail') return false;
            echo("MailCommand handling 'mail'
    ");
            return true;
        }
    }
    
    $cc = new CommandChain();
    $cc->addCommand(new UserCommand());
    $cc->addCommand(new MailCommand());
    $cc->runCommand('addUser', null);
    $cc->runCommand('mail', null);
    

      

    23种模式总览 : https://www.cnblogs.com/houss/p/11121584.html

  • 相关阅读:
    多播委托和匿名方法再加上Lambda表达式
    委托
    从警察抓小偷看委托
    StringBuilder
    C#修饰符详解
    数据结构与算法之队列
    数据结构与算法之栈
    win10重复安装
    网络编程基础
    PrintPreviewControl
  • 原文地址:https://www.cnblogs.com/houss/p/13246335.html
Copyright © 2011-2022 走看看