PHP设计模式入门之状态模式原理与实现方法分析(4)
WinnerState.php
<?php require_once 'State.php'; class WinnerState implements State { /** * 饮料机的实例 * * @var object */ private $_juiceMachine; /** * 构造方法,主要用于初始化饮料机实例 */ public function __construct($juiceMachine) { $this->_juiceMachine = $juiceMachine; } /* * (non-PHPdoc) @see State::insertCoin() */ public function insertCoin() { // TODO Auto-generated method stub echo "wait a minute, we are giving you a bottle of juice<br />"; } /* * (non-PHPdoc) @see State::retreatCoin() */ public function retreatCoin() { // TODO Auto-generated method stub echo "sorry, you already clicked the botton<br />"; } /* * (non-PHPdoc) @see State::clickButton() */ public function clickButton() { // TODO Auto-generated method stub echo "click twice does'nt get you two bottle of juice<br />"; } /* * (non-PHPdoc) @see State::dispend() */ public function dispend() { echo "you are a winner! you get two bottle of juice!<br />"; $this->_juiceMachine->decJuice(); if ($this->_juiceMachine->getCount() > 0) { $this->_juiceMachine->decJuice(); if ($this->_juiceMachine->getCount() <= 0) { echo "opps, runing out of juice<br />"; // 如果这时饮料机中没有饮料了,将饮料机的状态重置为销售一空 $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); } else { // 将饮料机的状态重置为没有钱 $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); } } else { echo "opps, runing out of juice<br />"; // 如果这时饮料机中没有饮料了,将饮料机的状态重置为销售一空 $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); } } }
JuiceMachine.php
<?php require_once './state/NomoneyState.php'; require_once './state/HasmoneyState.php'; require_once './state/SoldState.php'; require_once './state/SoldoutState.php'; require_once './state/WinnerState.php'; class JuiceMachine { /** * 记录糖果机当前的状态,初始化状态为售空 * * @var object */ private $_state; /** * 该变量用于记录饮料机中饮料的数量 */ private $_count; /** * 构造方法,最主要是用来初始化count和state属性的 */ public function __construct($count) { $this->_state = new SoldoutState($this); $this->_count = $count; // 当饮料机中的饮料数量大于零时,将饮料机的状态重置为没有钱的状态。 if ($this->_count > 0) { $this->_state = new NomoneyState($this); } } /* * (non-PHPdoc) @see State::insertCoin() */ public function insertCoin() { // TODO Auto-generated method stub $this->_state->insertCoin(); } /* * (non-PHPdoc) @see State::retreatCoin() */ public function retreatCoin() { // TODO Auto-generated method stub $this->_state->retreatCoin(); } /* * (non-PHPdoc) @see State::clickButton() */ public function clickButton() { $this->_state->clickButton(); //其实发放糖果是在用户点击完按钮后机器内部进行的所有没有必要再写一个dispend方法 $this->_state->dispend(); } /** * 设置糖果机的状态 * * @param State $state */ public function setState(State $state) { $this->_state = $state; } /** * 获取没有钱的状态 */ public function getNomoneyState(){ return new NomoneyState($this); } /** * 获取有钱的状态 */ public function getHasmoneyState(){ return new HasmoneyState($this); } /** * 获取售出的状态 */ public function getSoldState(){ return new SoldState($this); } /** * 获取销售一空的状态 */ public function getSoldoutState(){ return new SoldoutState($this); } /** * 获取幸运者的状态 */ public function getWinnerState(){ return new WinnerState($this); } /** * 获取饮料机中饮料的数量 */ public function getCount(){ return $this->_count; } /** * 将饮料数量减一 */ public function decJuice(){ echo "now you get you juice<br />"; //饮料机中的饮料数量减一 $this->_count--; } }
内容版权声明:除非注明,否则皆为本站原创文章。