Give me a real, non-trivial use of state design

I am looking for an example of where the state model was used to solve or simplify interesting or complex state transitions. There are many examples with three or four simple states. But what about code from real-life projects that have sub-states and more than a few transitions? The type of code that actually motivates the use of the template. Bonus points indicate a code!

+4
source share
2 answers

One real use of the state template that I have seen so far is a video player that can play online video.

You need to process games, pause, buffer, bind, search, and even other states.

When a player is in a playback state or paused, he responds to user interaction events.
When a player is in a β€œconnected” state, he may not have enough data to know the duration of the video, so the search bar should be turned off.
As soon as the player is connected, he will move to the buffering state. In the buffered state, the user can search or stop the video. But if he tries to pause or play the video, the command will be saved later, so when buffering is completed, the video will be paused or playback will start. and etc.

+11
source

Traffic light system (sensor response time [event] is triggered)

Status: RED, YELLOW, GREEN (simplest example)

Transitions: after changing the RED timer to green, green and yellow, and yellow to red.

+1
source

All Articles