<>Java Finite state machine ( Design patterns —— State mode )

When you write code , Sometimes we meet more complicated ones swith...case... and if...else... sentence . At this moment, I sometimes think of the state machine , Replace with finite state machine
swith...case... and if...else... sure :

* Reduce the complexity of the program ;
* Improve program maintainability ;
* State machine mode embodies the principle of opening and closing and the principle of single responsibility .
Each state is a subclass , Adding state means adding subclasses ; To change the state, just change a class .
These are the benefits of finite state machines . There are also shortcomings :

* The use of state machine subclasses increases , That is, like inflation , This needs to be measured by programmers themselves in development .
<> State mode definition :

Allow an object to alter its behavior when its internal state changes.The
object will appear to change its class.
Allows an object to change its behavior when its internal state changes . It looks like its class has been changed ( The translation is not good , This should be the embodiment of its encapsulation : External calls don't have to know how their internal state and behavior changes ).

<> for instance

We take the elevator every day , The elevator has four states : Open the door , close , function , stop it .

Col1 Open the door Act closed Behavior operation Cessation of action behavior
Open the door state noyesnono
close state yesnoyesyes
function state nononoyes
stop it state yesnoyesno
<>LiftState.java
/** * Defining elevator behavior : open , close , function , stop it */ public abstract class LiftState { //
Owning an elevator object , Used to update the current status of the elevator protected Lift mLift; /** * Introducing elevator instantiation object through constructor * * @param
lift */ public LiftState(Lift lift) { this.mLift = lift; } /** * behavior : Open the elevator door */
public abstract void open(); /** * behavior : Close the elevator door */ public abstract void close();
/** * behavior : Elevator operation */ public abstract void run(); /** * behavior : The elevator stops running */ public abstract
void stop(); }
<> Four states of elevator
public class OpeningState extends LiftState { public OpeningState(Lift lift) {
super(lift); } @Override public void open() { // Open the door System.out.println(
" Open the door "); } @Override public void close() { // Close the door // 1, Switch to closed state mLift.
setState(mLift.getCloseingState()); // 2, close mLift.close(); } @Override public
void run() { // do noting // Door opening state , Cannot perform run action } @Override public void stop() { //
do noting // When the door is opened , Do not perform stop action } } public class ClosingState extends LiftState {
public ClosingState(Lift lift) { super(lift); } @Override public void open() {
// Open the door // 1, Change to open state this.mLift.setState(mLift.getOpenningState()); // 2, Open the door
this.mLift.open(); } @Override public void close() { System.out.println(" Close the door "
); } @Override public void run() { // Operation action // 1, running state this.mLift.setState(mLift.
getRunningState()); // 2, Operation action this.mLift.run(); } @Override public void stop() {
// Stop action // 1, Change to stop state this.mLift.setState(mLift.getStoppingState()); // 2, stop it this
.mLift.stop(); } } public class RunningState extends LiftState { public
RunningState(Lift lift) { super(lift); } @Override public void open() { // do
noting } @Override public void close() { // do noting } @Override public void
run() { // Operation action System.out.println(" The elevator is running up and down ..."); } @Override public void stop()
{ // Stop action // 1, Change to stop state this.mLift.setState(mLift.getStoppingState()); // 2, Stop action
this.mLift.stop(); } } public class StoppingState extends LiftState { public
StoppingState(Lift lift) { super(lift); } @Override public void open() { // Door opening action
// 1, Door opening state this.mLift.setState(mLift.getOpenningState()); // 2, Open the door this.mLift.
open(); } @Override public void close() { // do noting } @Override public void
run() { // Operation action // 1, running state this.mLift.setState(mLift.getRunningState()); //
2, Operation action this.mLift.run(); } @Override public void stop() { // Elevator stop action System.out.
println(" The elevator stops running ..."); } }
<> Define elevator class
/** * Define elevator class */ public class Lift { // Define all the states of the elevator private LiftState openningState
; private LiftState closingState; private LiftState runningState; private
LiftState stoppingState; // Define current elevator status private LiftState mCurState; /** * Construction method */
public Lift() { openningState = new OpeningState(this); closingState = new
ClosingState(this); runningState = new RunningState(this); stoppingState = new
StoppingState(this); } /** * Open the door */ public void open() { mCurState.open(); }
/** * Close the door */ public void close() { mCurState.close(); } /** * Execute running action */
public void run() { mCurState.run(); } /** * Execute stop action */ public void stop() {
mCurState.stop(); } // ################## Set current elevator status ##################### /** *
Set current elevator status * * @param state */ public void setState(LiftState state) { this.
mCurState= state; } // ################### Get all status of elevator #################### public
LiftStategetOpenningState() { return openningState; } public LiftState
getCloseingState() { return closingState; } public LiftState getRunningState() {
return runningState; } public LiftState getStoppingState() { return
stoppingState; } }
<> function
public static void main(String[] args) { Lift lift = new Lift(); lift.setState(
new ClosingState(lift)); lift.open(); lift.close(); lift.run(); lift.stop(); }
<> Operation results
Open the door Close the door The elevator is running up and down ... The elevator stops running ...
<> reference resources :

《 Zen of design pattern 》

<>========== THE END ==========

Technology