标签:
<?php class Light { private $offState; //关闭状态 private $onState; //开启状态 private $currentState; //当前状态 public function __construct() { $this->offState = new OffState($this); $this->onState = new OnState($this); //开始状态为关闭状态Off $this->currentState = $this->offState; } //调用状态方法触发器 public function turnLightOn() { $this->currentState->turnLightOn(); } public function turnLightOff() { $this->currentState->turnLightOff(); } //设置当前状态 public function setState(IState $state) { $this->currentState = $state; } //获取状态 public function getOnState() { return $this->onState; } public function getOffState() { return $this->offState; } }
$this->offState = new OffState($this); $this->onState = new OnState($this);
<?php interface IState { public function turnLightOn(); public function turnLightOff(); }
<?php class OnState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "灯已经打开了->不做操作<br />"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOffState()); } }
<?php class OffState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "灯打开!可以看见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOnState()); } public function turnLightOff() { echo "灯已经关闭了->不做操作<br />"; } }
<?php function __autoload($class_name) { include_once $class_name.‘.php‘; } class Client { private $light; public function __construct() { $this->light = new Light(); $this->light->turnLightOn(); $this->light->turnLightOn(); $this->light->turnLightOff(); $this->light->turnLightOff(); } } $worker = new Client();
<?php interface IState { public function turnLightOn(); public function turnLightOff(); public function turnBrighter(); public function turnBrightest(); }
<?php class OnState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "不合法的操作!<br />"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOffState()); } public function turnBrighter() { echo "灯更亮了, 看帅哥chenqionghe看得更真切了!<br />"; $this->light->setState($this->light->getBrighterState()); } public function turnBrightest() { echo "不合法的操作!<br />"; } }
<?php class OffState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "灯打开!可以看见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOnState()); } public function turnLightOff() { echo "不合法的操作!<br />"; } public function turnBrighter() { echo "不合法的操作!<br />"; } public function turnBrightest() { echo "不合法的操作!<br />"; } }
<?php class BrighterState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "不合法的操作!<br />"; } public function turnLightOff() { echo "不合法的操作!<br />"; } public function turnBrighter() { echo "不合法的操作!<br />"; } public function turnBrightest() { echo "灯最亮了, 看帅哥chenqionghe已经帅到无敌!<br />"; $this->light->setState($this->light->getBrightestState()); } }
<?php class BrightestState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "灯已经打开了->不做操作<br />"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOffState()); } public function turnBrighter() { echo "不合法的操作!<br />"; } public function turnBrightest() { echo "不合法的操作!<br />"; } }
<?php class Light { private $offState; //关闭状态 private $onState; //开启状态 private $brighterState; //更亮状态 private $brightestState;//最亮状态 private $currentState; //当前状态 public function __construct() { $this->offState = new OffState($this); $this->onState = new OnState($this); $this->brighterState = new BrighterState($this); $this->brightestState = new BrightestState($this); //开始状态为关闭状态Off $this->currentState = $this->offState; } //调用状态方法触发器 public function turnLightOn() { $this->currentState->turnLightOn(); } public function turnLightOff() { $this->currentState->turnLightOff(); } public function turnLightBrighter() { $this->currentState->turnBrighter(); } public function turnLigthBrightest() { $this->currentState->turnBrightest(); } //设置当前状态 public function setState(IState $state) { $this->currentState = $state; } //获取状态 public function getOnState() { return $this->onState; } public function getOffState() { return $this->offState; } public function getBrighterState() { return $this->brighterState; } public function getBrightestState() { return $this->brightestState; } }
<?php function __autoload($class_name) { include_once $class_name.‘.php‘; } class Client { private $light; public function __construct() { $this->light = new Light(); $this->light->turnLightOn(); $this->light->turnLightBrighter(); $this->light->turnLigthBrightest(); $this->light->turnLightOff(); $this->light->turnLigthBrightest(); } } $worker = new Client();
灯打开!可以看见帅哥chenqionghe了! 灯更亮了, 看帅哥chenqionghe看得更真切了! 灯最亮了, 看帅哥chenqionghe已经帅到无敌! 灯关闭!看不见帅哥chenqionghe了! 不合法的操作!
<?php
<?php interface IMatrix { public function goUp(); public function goDown(); public function goLeft(); public function goRight(); }
<?php class Context { private $cell1; private $cell2; private $cell3; private $cell4; private $cell5; private $cell6; private $cell7; private $cell8; private $cell9; private $currentState; public function __construct() { $this->cell1 = new Cell1State($this); $this->cell2 = new Cell2State($this); $this->cell3 = new Cell3State($this); $this->cell4 = new Cell4State($this); $this->cell5 = new Cell5State($this); $this->cell6 = new Cell6State($this); $this->cell7 = new Cell7State($this); $this->cell8 = new Cell8State($this); $this->cell9 = new Cell9State($this); $this->currentState = $this->cell5; } //调用方法 public function doUp() { $this->currentState->goUp(); } public function doDown() { $this->currentState->goDown(); } public function doLeft() { $this->currentState->goLeft(); } public function doRight() { $this->currentState->goRight(); } //设置当前状态 public function setState(IMatrix $state) { $this->currentState = $state; } //获取状态 public function getCell1State() { return $this->cell1; } public function getCell2State() { return $this->cell2; } public function getCell3State() { return $this->cell3; } public function getCell4State() { return $this->cell4; } public function getCell5State() { return $this->cell5; } public function getCell6State() { return $this->cell6; } public function getCell7State() { return $this->cell7; } public function getCell8State() { return $this->cell8; } public function getCell9State() { return $this->cell9; } }
<?php class Cell1State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘不合法的移动!<br />‘; } public function goRight() { echo ‘走到<strong>2</strong><br />‘; $this->context->setState($this->context->getCell2State()); } public function goUp() { echo ‘不合法的移动!<br />‘; } public function goDown() { echo ‘走到<strong>4</strong><br />‘; $this->context->setState($this->context->getCell4State()); } }
<?php class Cell2State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘走到<strong>1</strong><br />‘; $this->context->setState($this->context->getCell1State()); } public function goRight() { echo ‘走到<strong>3</strong><br />‘; $this->context->setState($this->context->getCell3State()); } public function goUp() { echo ‘不合法的移动!<br />‘; } public function goDown() { echo ‘走到<strong>5</strong><br />‘; $this->context->setState($this->context->getCell5State()); } }
<?php class Cell3State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘走到<strong>2</strong><br />‘; $this->context->setState($this->context->getCell2State()); } public function goRight() { echo ‘不合法的移动!<br />‘; } public function goUp() { echo ‘不合法的移动!<br />‘; } public function goDown() { echo ‘走到<strong>6</strong><br />‘; $this->context->setState($this->context->getCell6State()); } }
<?php class Cell4State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘不合法的移动!<br />‘; } public function goRight() { echo ‘走到<strong>5</strong><br />‘; $this->context->setState($this->context->getCell5State()); } public function goUp() { echo ‘走到<strong>1</strong><br />‘; $this->context->setState($this->context->getCell1State()); } public function goDown() { echo ‘走到<strong>7</strong><br />‘; $this->context->setState($this->context->getCell7State()); } }
<?php class Cell5State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘走到<strong>4</strong><br />‘; $this->context->setState($this->context->getCell4State()); } public function goRight() { echo ‘走到<strong>6</strong><br />‘; $this->context->setState($this->context->getCell6State()); } public function goUp() { echo ‘走到<strong>2</strong><br />‘; $this->context->setState($this->context->getCell2State()); } public function goDown() { echo ‘走到<strong>8</strong><br />‘; $this->context->setState($this->context->getCell8State()); } }
<?php class Cell6State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘走到<strong>5</strong><br />‘; $this->context->setState($this->context->getCell5State()); } public function goRight() { echo ‘不合法的移动!<br />‘; } public function goUp() { echo ‘走到<strong>3</strong><br />‘; $this->context->setState($this->context->getCell3State()); } public function goDown() { echo ‘走到<strong>9</strong><br />‘; $this->context->setState($this->context->getCell9State()); } }
<?php class Cell7State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘不合法的移动!<br />‘; } public function goRight() { echo ‘走到<strong>8</strong><br />‘; $this->context->setState($this->context->getCell8State()); } public function goUp() { echo ‘走到<strong>4</strong><br />‘; $this->context->setState($this->context->getCell4State()); } public function goDown() { echo ‘不合法的移动!<br />‘; } }
<?php class Cell8State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘走到<strong>7</strong><br />‘; $this->context->setState($this->context->getCell7State()); } public function goRight() { echo ‘走到<strong>9</strong><br />‘; $this->context->setState($this->context->getCell9State()); } public function goUp() { echo ‘走到<strong>5</strong><br />‘; $this->context->setState($this->context->getCell5State()); } public function goDown() { echo ‘不合法的移动!<br />‘; } }
<?php class Cell9State implements IMatrix { private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function goLeft() { echo ‘走到<strong>8</strong><br />‘; $this->context->setState($this->context->getCell8State()); } public function goRight() { echo ‘不合法的移动!<br />‘; } public function goUp() { echo ‘走到<strong>6</strong><br />‘; $this->context->setState($this->context->getCell6State()); } public function goDown() { echo ‘不合法的移动!<br />‘; } }
<?php function __autoload($class_name) { include_once $class_name.‘.php‘; } class Client { private $context; public function __construct() { $this->context = new Context(); $this->context->doUp(); $this->context->doRight(); $this->context->doDown(); $this->context->doDown(); $this->context->doLeft(); $this->context->doUp(); } } $worker = new Client();
运行结果如下
走到2 走到3 走到6 走到9 走到8 走到5
标签:
原文地址:http://www.cnblogs.com/chenqionghe/p/4804259.html