public static function main(array $args = array())
 {
     $remote = new SimpleRemoteControl();
     $light = new Light();
     $garageDoor = new GarageDoor();
     $lightOn = new LightOnCommand($light);
     $garageOpen = new GarageDoorOpenCommand($garageDoor);
     $remote->setCommand($lightOn);
     $remote->buttonWasPressed();
     $remote->setCommand($garageOpen);
     $remote->buttonWasPressed();
 }
Esempio n. 2
0
<?php

ini_set('display_errors', "On");
error_reporting(E_ALL | E_STRICT);
//自动加载类
set_include_path(get_include_path() . PATH_SEPARATOR . 'class/');
//设置加载路径
spl_autoload_extensions('.php');
//设置加载后缀名
function myAutoload($className)
{
    require_once $className . '.php';
    //直接根据类名跟文件关系包含文件
}
spl_autoload_register("myAutoload");
//注册自动加载函数
//测试代码开始
//整段测试代码是命令模式的客户
$remoteControl = new SimpleRemoteControl();
//遥控器,调用者
$light = new Light();
//对象,请求接收者
$lightOnCommand = new CommandLightOn($light);
//创建命令,将命令接收者传给它
$remoteControl->setCommand($lightOnCommand);
//把命令传给调用者
$remoteControl->buttonWasPressed();
//模拟按下按钮
echo '<hr>';
echo file_get_contents('./read.txt');
Esempio n. 3
0
class SimpleRemoteControl
{
    public $slot;
    #插槽:一个插槽对应一个请求
    public function setCommand($command)
    {
        $this->slot = $command;
    }
    public function bottonWasPressd()
    {
        $this->slot->execute();
    }
}
println(SEPARATE, '命令模式', SEPARATE);
showImgs('6.png');
$control = new SimpleRemoteControl();
$light = new Light();
$command = new LightOnCommand($light);
$control->setCommand($command);
$control->bottonWasPressd();
/**
* 输出:
 Light on
*/
class RemoteControl
{
    public $onCommands = array();
    public $offCommands = array();
    public $lastCommand;
    #最后一个命令
    public function __construct()
Esempio n. 4
0
<?php

function __autoload($class_name)
{
    $parts = explode("\\", $class_name);
    require_once implode(DIRECTORY_SEPARATOR, $parts) . '.php';
}
//Simple Remote
$remote = new SimpleRemoteControl();
$light = new Machine\Light("Corridor Light");
$lightOn = new Command\LightOnCommand($light);
$remote->setCommand($lightOn);
$remote->buttonWasPressed();
//Remote Control
$re = new RemoteControl();
$livingRoom = new Machine\Light('Living Room');
$kitchenLight = new Machine\Light('Kitchen');
$ceilingFan = new Machine\CeilingFan('Living Room');
$garageDoor = new Machine\GarageDoor('Main');
$stereo = new Machine\Stereo('My Room');
$re->setCommand(0, new Command\LightOnCommand($livingRoom), new Command\LightOffCommand($livingRoom));
$re->setCommand(1, new Command\LightOnCommand($kitchenLight), new Command\LightOffCommand($kitchenLight));
$re->setCommand(2, new Command\CeilingFanHighCommand($ceilingFan), new Command\CeilingFanOffCommand($ceilingFan));
$re->setCommand(3, new Command\GarageDoorOnCommand($garageDoor), new Command\GarageDoorOffCommand($garageDoor));
$re->setCommand(4, new Command\StereoOnWithCDCommand($stereo), new Command\StereoOffCommand($stereo));
echo $re;
$re->onButtonWasPushed(0);
$re->offButtonWasPushed(0);
$re->onButtonWasPushed(1);
$re->undoButtonWasPushed();
$re->offButtonWasPushed(1);
Esempio n. 5
0
<?php

require_once "devices/Light.php";
require_once "Command.php";
require_once "LightOnCommand.php";
require_once "SimpleRemoteControl.php";
//--------------------------------------------------------
//Создаём пульт управления
$simpleRemoteControl = new SimpleRemoteControl();
echo "<b>Start remote control.</b><hr>";
//Создаём новую команду - включить свет
$light = new Light();
$myCommand = new LightOnCommand($light);
$simpleRemoteControl->setCommand($myCommand);
$simpleRemoteControl->buttonWasPressed();