PHP设计模式入门之状态模式原理与实现方法分析(3)
HasmoneyState.php
<?php
require_once 'State.php';
class HasmoneyState 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 "you can't insert another coin!<br />";
}
/*
* (non-PHPdoc) @see State::retreatCoin()
*/
public function retreatCoin()
{
// TODO Auto-generated method stub
echo "coin return!<br />";
$this->_juiceMachine->setState($this->_juiceMachine->getNomoneyState());
}
/*
* (non-PHPdoc) @see State::clickButton()
*/
public function clickButton()
{
// TODO Auto-generated method stub
echo "you clicked, we are giving you a bottle of juice...<br />";
// 改变饮料机的状态为售出模式
$rand = mt_rand(0, 0);
// 当随机数为0(即1/10的概率)并且饮料机中还有1瓶以上的饮料时
if ($rand == 0 && $this->_juiceMachine->getCount() > 1) {
$this->_juiceMachine->setState($this->_juiceMachine->getWinnerState());
} else {
$this->_juiceMachine->setState($this->_juiceMachine->getSoldState());
}
}
/*
* (non-PHPdoc) @see State::dispend()
*/
public function dispend()
{
// TODO Auto-generated method stub
echo "please click the button first<br />";
}
}
SoldoutState.php
<?php
require_once 'State.php';
class SoldoutState 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 "you can't insert coin, the machine is already soldout<br />";
}
/* (non-PHPdoc)
* @see State::retreatCoin()
*/
public function retreatCoin()
{
// TODO Auto-generated method stub
echo "you have'nt inserted a coin yet<br />";
}
/* (non-PHPdoc)
* @see State::clickButton()
*/
public function clickButton()
{
// TODO Auto-generated method stub
echo "you clicked, but the machine is already soldout<br />";
}
/* (non-PHPdoc)
* @see State::dispend()
*/
public function dispend()
{
// TODO Auto-generated method stub
echo "opps, it appears that we don't have any juice left<br />";
}
}
SoldState.php
<?php
require_once 'State.php';
class SoldState 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()
{
$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->getNomoneyState());
}
}
}
内容版权声明:除非注明,否则皆为本站原创文章。
