...

<?php
/*
The command pattern decouples the object that executes certain operations from
objects that know how to use it. It does so by encapsulating all of the relevant
information needed for later execution of a certain action. This implies information
about object, method name, and method parameters.
*/

interface LightBulbCommand {
    public function execute();
}

class LightBulbControl {
    public function turnOn() {
        echo 'LightBuld turnOn.<br/>';
    }

    public function turnOff() {
        echo 'LightBuld turnOff.<br/>';
    }
}

class TurnOnLightBulb implements LightBulbCommand {
    private $lightBulbControl;

    public function __construct(LightBulbControl
        $lightBulbControl) {
            $this->lightBulbControl = $lightBulbControl;
        }

    public function execute() {
        $this->lightBulbControl->turnOn();
    }
}

class TurnOffLightBulb implements LightBulbCommand {
    private $lightBulbControl;

    public function __construct(LightBulbControl
        $lightBulbControl) {
            $this->lightBulbControl = $lightBulbControl;
        }

    public function execute() {
        $this->lightBulbControl->turnOff();
    }
}

$command = new TurnOffLightBulb(new LightBulbControl());
$command->execute();

$command = new TurnOnLightBulb(new LightBulbControl());
$command->execute();
?>

php命令模式(command pattern)的更多相关文章

  1. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  2. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...

  3. 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释

    命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...

  4. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  5. 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释

    命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...

  6. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  7. 设计模式-15命令模式(Command Pattern)

    1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...

  8. 设计模式(六):控制台中的“命令模式”(Command Pattern)

    今天的博客中就来系统的整理一下“命令模式”.说到命令模式,我就想起了控制台(Console)中的命令.无论是Windows操作系统(cmd.exe)还是Linux操作系统(命令行式shell(Comm ...

  9. 十一个行为模式之命令模式(Command Pattern)

    定义: 将一个请求封装成对象,使得请求发送者和请求接受者之间相互隔离,消除两者之间的耦合.引入命令类,使得不同请求对客户参数化,并且可以对命令添加附件操作,如:排队.撤销.日志.组合等. 结构图: C ...

  10. Swift语言之命令模式(Command Pattern)实现

    今天遇到这样一个问题,我现在有一个整数数组,如: var numbers = [3, 7, 12, 9, 200] 现需要对其中的每一个数字都执行一系列相同的加减乘除操作,如对每一个数字都加5乘8再减 ...

随机推荐

  1. js追加html元素

    jquery追加html代码,添加元素 .append() //新增仲裁申请人 $("."+inputName).append("<div class=\" ...

  2. Tarjan 算法求 LCA / Tarjan 算法求强连通分量

    [时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...

  3. c# 创建socket连接辅助类

    using AD.SocketForm.Model; using NLog; using System; using System.Net; using System.Net.Sockets; nam ...

  4. Maven 教程(12)— Maven生命周期和插件

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79549695 除了坐标.依赖以及仓库之外,Maven的另外两个核心概念是生命周期 ...

  5. hanlp进行命名实体识别

    需要安装jpype先,这个是python调用java库的桥梁. # -*- coding: utf-8 -*- """ Created on Thu May 10 09: ...

  6. ES6高级技巧(二)

    Array.from const cities = [ { name: 'Milan', visited: 'no' }, { name: 'Palermo', visited: 'yes' }, { ...

  7. 本周使用angular7所遇到的一些问题

    前言 本周在使用angular7所遇到的一些问题,学习是不断的循序渐进的过程,在本周完成对应的工作后,也要抽出一些时间用来学习,比较我们公司10点上班,我一般9点就会到,在这一个小时内看看博客,写写笔 ...

  8. 创建新react项目 运行npm start 报错踩过的坑

    1.看react官网创建新的react项目 :npx create-react-app my-app    cd到my-app  npm start 遇见如下报错 这是因为电脑本地git的原因 ,不是 ...

  9. AFO啦!

    \(\mathrm{CSP}2019\)结束了,我的信息生活也要结束了,从五年级开始的信息学习,陪伴我度过了很多,今天要放手,全心学习文化课了. 信息学,再见!

  10. AQS原理解析 AbstractQueuedSynchronizer

    AQS实现原理  https://blog.csdn.net/ym123456677/article/details/80381354   https://www.cnblogs.com/keleli ...