gogoWebsite

Design Mode-Status Mode State-Elevator Status

Updated to 16 days ago
  • <?php  
  • /** 
  •  *  
  • * Define the interface of an elevator 
  •  */   
  • abstract class LiftState{  
  •   
  •     //Define an environment role, that is, the function changes caused by the transformation of the encapsulation state  
  •     protected  $_context;  
  •   
  •     public function setContext(Context $context){  
  •         $this->_context = $context;  
  •     }  
  •   
  •     //First, the elevator door starts  
  •     public abstract function open();  
  •   
  •     //The elevator door is opened, so of course it will be closed  
  •     public abstract function close();  
  •   
  •     //The elevator must be able to go up and down, and run  
  •     public abstract function run();  
  •   
  •     //If the elevator can still be stopped, it would be nonsense if it can't be stopped.  
  •     public abstract function 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;  
  •   
  •     public function __construct() {  
  •         self::$openningState = new OpenningState();  
  •         self::$closeingState = new ClosingState();  
  •         self::$runningState =  new RunningState();  
  •         self::$stoppingState = new StoppingState();  
  •   
  •     }  
  •   
  •     //Corresize a current elevator status  
  •     private  $_liftState;  
  •   
  •     public function getLiftState() {  
  •         return $this->_liftState;  
  •     }  
  •   
  •     public function setLiftState($liftState) {  
  •         $this->_liftState = $liftState;  
  •         //Notify the current environment to each implementation class  
  •         $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();  
  •     }  
  • }  
  •   
  • /** 
  • * 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 
  •      * 
  •      */  
  •     public function close() {  
  •         //Status modification  
  •         $this->_context->setLiftState(Context::$closeingState);  
  •         //The action delegate is to CloseState to execute  
  •         $this->_context->getLiftState()->close();  
  •     }  
  •   
  •     //Open the elevator door  
  •     public function open() {  
  •         echo 'lift open...''<br/>';  
  •     }  
  •     //The elevator is just about to run when the door is open. This elevator will scare you to death!  
  •     public function run() {  
  •         //do nothing;  
  •     }  
  •   
  •     //Don't you stop opening the door?  
  •     public function stop() {  
  •         //do nothing;  
  •     }  
  •   
  • }  
  •   
  • /** 
  • * What can the elevator do after the elevator door is closed? 
  •  */   
  • class ClosingState extends LiftState {  
  •   
  •     //The elevator door is closed, this is the action to be realized in the closed state  
  •     public function close() {  
  •         echo 'lift close...''<br/>';  
  •   
  •     }  
  •     //The elevator door is closed and then open it. I am having fun, so this is allowed  
  •     public function open() {  
  •         $this->_context->setLiftState(Context::$openningState);  //Set the door open  
  •         $this->_context->getLiftState()->open();  
  •     }  
  •   
  •     //If the elevator door is closed, run away. This is normal  
  •     public function run() {  
  •         $this->_context->setLiftState(Context::$runningState); //Set to run state;  
  •         $this->_context->getLiftState()->run();  
  •     }  
  •   
  •     //The elevator door is closed, I won't press the floor  
  •       
  •     public function stop() {  
  •         $this->_context->setLiftState(Context::$stoppingState);  //Set to stop state;  
  •         $this->_context->getLiftState()->stop();  
  •     }  
  •   
  • }  
  •   
  • /** 
  • * What actions can the elevator do in operation? 
  •  */   
  • class RunningState extends LiftState {  
  •   
  •     //The elevator door is closed? This is for sure  
  •     public function close() {  
  •         //do nothing  
  •     }  
  •   
  •     //Open the elevator door when running? You're crazy! The elevator won't be driven for you  
  •     public function open() {  
  •         //do nothing  
  •     }  
  •   
  •     //This is the method to implement in the running state  
  •     public function run() {  
  •         echo 'lift run...''<br/>';  
  •     }  
  •   
  •     //This matter is absolutely reasonable. Who dares to do this elevator if you don’t stop running? ! It's probably only God  
  •     public function stop() {  
  •         $this->_context->setLiftState(Context::$stoppingState); //The environment is set to stop state;  
  •         $this->_context->getLiftState()->stop();  
  •     }  
  •   
  • }  
  •   
  •   
  •   
  • /** 
  • * What can you do in a stop state 
  •  */   
  • class StoppingState extends LiftState {  
  •   
  •     //Stop state closes? The elevator door was originally closed!  
  •     public function close() {  
  •         //do nothing;  
  •     }  
  •   
  •     //Stop, open the door, that's what you want!  
  •     public function open() {  
  •         $this->_context->setLiftState(Context::$openningState);  
  •         $this->_context->getLiftState()->open();  
  •     }  
  •     //Stop and run again, it's normal  
  •     public function run() {  
  •         $this->_context->setLiftState(Context::$runningState);  
  •         $this->_context->getLiftState()->run();  
  •     }  
  •     //How does the stop state occur? Of course, it's the stop method.  
  •     public function stop() {  
  •         echo 'lift stop...''<br/>';  
  •     }  
  •   
  • }  
  •   
  • /** 
  • * Simulate the movement of the elevator 
  •  */   
  • class Client {  
  •   
  •     public static function main() {  
  •         $context = new Context();  
  •         $context->setLiftState(new ClosingState());  
  •   
  •         $context->open();  
  •         $context->close();  
  •         $context->run();  
  •         $context->stop();  
  •     }  
  • }  
  • Client::main();