具有先前状态的状态模式 C#
我是 C# 中状态模式实现的新手,您能否提供一些有关如何实现它的信息。
我正在使用状态模式在 C# 中重构状态机。目前我的状态机包含 5 个状态,并且只能在这些状态中前进或后退,即从状态 1 需要转到状态 2、3 和 4,最终到达状态 5。
我可以继续执行,
mainclass.State = new NextSate();
每次您想要继续前进时都会创建一个新状态,但是,一旦所有状态都已完成创建和/或如果你想倒退,我需要去同样的州,而不仅仅是一个新的州。我怎样才能做到这一点?有没有更好的办法可以简单点呢?
I am new to the state pattern implementation in C#, could you provide some info on how you implement it.
I am refactoring a state machine in C# using the state pattern. Currently my state machine contains 5 states and it is only possible to go forward or backward througout the states,i.e. from state 1 you need to go to state 2, 3 and 4 to finally arrive to state 5.
I am able to go forward just doing
mainclass.State = new NextSate();
which creates a new state every time you want to go forward, however, once all of them have been created and/or you want to go backward I would need to go to the same states, not just a new one. How can I do that? Is there any better way to do it simple?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
严格来说,如果您正在实现经典的 GoF 状态模式,那么状态子类本身负责了解并执行状态转换。 State 的持有者不负责管理转换,该模式的很大一部分目的是将状态转换行为封装在 State 对象中,从而让客户端委托给它们。我引入了一个工厂,它确保每个状态子类只有一个实例,以确保在状态之间来回移动时重复使用同一实例。
Strictly speaking, if you're implementing the classic GoF State pattern then the State subclasses themselves are responsible for knowing about and performing the State transitions. The holder of the State isn't responsible for managing the transitions and a large part of the intent of the pattern is to encapsulate the state transition behaviour in the State objects and thus for the client to delegate to them. I've introduced a Factory that ensures that there is only ever a single instance of each State subclass to ensure that the same instance is reused when moving back and forth through the states.
使用内部堆栈来维持先前的状态:
如果您想维持向前和向后的状态,则创建额外的堆栈:
Use internal stack to maintain the previous states:
if you want to maintain forward and backward states then create additional stack:
你有某种状态经理吗?如果是这样,该实例就可以保存状态实例。通过将状态转换知识与状态本身解耦,您可以让经理决定转换。管理器将检查请求转换的状态:它确定它是“步骤 1”状态,并返回(或创建)“状态 2”状态。
Do you have a state manager of some kind? If so, that one could hold the state instances. By decoupling the state transition knowledge from the states themselves, you let the manager decide on the transition. The manager will inspect the state that requested the transition: it determines it is the "step 1" state, and returns (or creates) the "state 2" state.