给我一个现实生活中状态模式的重要用途
我正在寻找一个示例,说明状态设计模式用于解决或简化有趣或复杂的状态转换。有很多包含三个或四个简单状态的示例。但是,来自现实生活项目的具有子状态和多个转换的代码又如何呢?真正激发该模式使用的代码类型。奖励积分指向代码!
I'm looking for an example of where the State design pattern has been used to solve or simplify interesting or complicated state transitions. There are plenty of examples with three or four simple states. But what about code from real life projects that have sub-states and more than a handful of transitions? The kind of code that actually motivates use of the pattern. Bonus points pointers to code!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,我所看到的状态模式在现实生活中的一种用法是在能够播放在线视频的视频播放器中。
您必须处理播放、暂停、缓冲、连接、搜索甚至其他状态。
当播放器处于播放或暂停状态时,它会响应用户交互事件。
当播放器处于“连接”状态时,它可能没有足够的数据来了解视频持续时间,因此应禁用搜索栏。
一旦播放器连接,它将进入缓冲状态。在缓冲状态下,用户可以寻找或停止视频。但是,如果他尝试暂停或播放视频,该命令将被保存以供稍后使用,以便在缓冲完成后,视频要么暂停,要么开始播放。 ETC。
One real-life use of State Pattern I have seen so far is in a Video Player capable of playing online video.
You have to handle playing, paused, buffering, connecting, seeking and even other states.
When the player is in playing or paused states, it is responsive to user interaction events.
When the player is in "connecting" state, it might not have enough data to know the video duration and so the seek bar should be disabled.
Once the player is connected, it will move to buffering state. In buffering state, the user can seek, or stop the video. But if he tries to pause or play the video, the command is saved for later, so that when the buffering is done, the video is either paused or start playing. etc.
交通灯系统(时间触发 | 传感器[事件]触发)
状态:红色、黄色、绿色(最简单的示例)
转换:计时器之后将红色更改为绿色,将绿色更改为黄色,将黄色更改为红色。
Traffic Light System (time triggered | sensor [event] triggered)
States: RED, YELLOW, GREEN (simplest example)
Transitions: After a timer change RED to GREEN, GREEN to YELLOW, and YELLOW to RED.