//Define an environment role, that is, the function changes caused by the transformation of the encapsulation state
protected$_context;
publicfunction setContext(Context $context){
$this->_context = $context;
}
//First, the elevator door starts
publicabstractfunction open();
//The elevator door is opened, so of course it will be closed
publicabstractfunction close();
//The elevator must be able to go up and down, and run
publicabstractfunction run();
//If the elevator can still be stopped, it would be nonsense if it can't be stopped.
publicabstractfunction stop();
}
/**
* Environment class: Defines the interface that customers are interested in. Maintain an instance of the ConcreteState subclass that defines the current state.
*/
class Context {
//Define all elevator status
static$openningState = null;
static$closeingState = null;
static$runningState = null;
static$stoppingState = null;
publicfunction __construct() {
self::$openningState = new OpenningState();
self::$closeingState = new ClosingState();
self::$runningState = new RunningState();
self::$stoppingState = new StoppingState();
}
//Corresize a current elevator status
private$_liftState;
publicfunction getLiftState() {
return$this->_liftState;
}
publicfunction setLiftState($liftState) {
$this->_liftState = $liftState;
//Notify the current environment to each implementation class
$this->_liftState->setContext($this);
}
publicfunction open(){
$this->_liftState->open();
}
publicfunction close(){
$this->_liftState->close();
}
publicfunction run(){
$this->_liftState->run();
}
publicfunction stop(){
$this->_liftState->stop();
}
}
/**
* What can you do when the elevator door is open?
*/
class OpenningState extends LiftState {
/**
* Of course it can be turned off when it is turned on, I just want to test the elevator door switch function