<?php
abstract class LiftState{
protected $_context;
public function setContext(Context $context){
$this->_context = $context;
}
public abstract function open();
public abstract function close();
public abstract function run();
public abstract function stop();
}
class Context {
static $openningState = null;
static $closeingState = null;
static $runningState = null;
static $stoppingState = null;
public function __construct() {
self::$openningState = new OpenningState();
self::$closeingState = new ClosingState();
self::$runningState = new RunningState();
self::$stoppingState = new StoppingState();
}
private $_liftState;
public function getLiftState() {
return $this->_liftState;
}
public function setLiftState($liftState) {
$this->_liftState = $liftState;
$this->_liftState->setContext($this);
}
public function open(){
$this->_liftState->open();
}
public function close(){
$this->_liftState->close();
}
public function run(){
$this->_liftState->run();
}
public function stop(){
$this->_liftState->stop();
}
}
class OpenningState extends LiftState {
public function close() {
$this->_context->setLiftState(Context::$closeingState);
$this->_context->getLiftState()->close();
}
public function open() {
echo 'lift open...', '<br/>';
}
public function run() {
}
public function stop() {
}
}
class ClosingState extends LiftState {
public function close() {
echo 'lift close...', '<br/>';
}
public function open() {
$this->_context->setLiftState(Context::$openningState);
$this->_context->getLiftState()->open();
}
public function run() {
$this->_context->setLiftState(Context::$runningState);
$this->_context->getLiftState()->run();
}
public function stop() {
$this->_context->setLiftState(Context::$stoppingState);
$this->_context->getLiftState()->stop();
}
}
class RunningState extends LiftState {
public function close() {
}
public function open() {
}
public function run() {
echo 'lift run...', '<br/>';
}
public function stop() {
$this->_context->setLiftState(Context::$stoppingState);
$this->_context->getLiftState()->stop();
}
}
class StoppingState extends LiftState {
public function close() {
}
public function open() {
$this->_context->setLiftState(Context::$openningState);
$this->_context->getLiftState()->open();
}
public function run() {
$this->_context->setLiftState(Context::$runningState);
$this->_context->getLiftState()->run();
}
public function stop() {
echo 'lift stop...', '<br/>';
}
}
class Client {
public static function main() {
$context = new Context();
$context->setLiftState(new ClosingState());
$context->open();
$context->close();
$context->run();
$context->stop();
}
}
Client::main();