[英]Appropriate Data Structure for Stages - enum?
我有两个类:抽认卡和集合。 抽认卡处于七个阶段之一-每个阶段都应具有TimeSpan属性。 目标是在特定时间段后显示卡,具体取决于卡所处的阶段。
一组也处于这七个阶段之一-所有卡中的最低阶段。
目前这两个类都具有“ public int Stage”属性,但我觉得这并不理想。
根据类/对象定义,什么是合适的建模方法? 如果有问题,我正在使用MVC4 EF CodeFirst。
状态机: http : //en.wikipedia.org/wiki/State_pattern 。
基本上,您有一个状态环(所有状态都实现一个接口)。 然后,让状态转换程序类实现一个“参与者”接口,如下所示:
// All stages should implement this
interface IStateRing
{
}
// Everything participating in the state ring should implement this
interface IStateRingParticipant
{
void SetStage(IStateRing stage);
}
参与者班级的唯一工作是在状态指示时响应状态变化,例如:
class MyStageParticipant : IStateRingParticipant
{
// Keep track of the current state.
IStateRing currentStage;
// This is called by the state when it's time to change
public void SetStage(IStateRing stage)
{
throw new NotImplementedException();
}
}
请注意,它会跟踪它所处的状态,然后有一个由当前状态调用的SetStage函数。 接下来,我们有我们的状态:
// The state handles the actual functionality, and when it's good and ready
// it commands the participant to change states.
class StateA : IStateRing
{
// Keep track of the participant.
IStateRingParticipant participant;
// The constructor should know what object belongs to this state.
// that is, which object is participating.
public StateA(IStateRingParticipant participant)
{
this.participant = participant;
}
// We do all of our processing in this state.
// Then when it's time we simply let go of the participant and
// instantiate a new state.
public void GoodAndReady()
{
new StateB(participant);
}
}
class StateB : IStateRing
{
IStateRingParticipant participant;
public StateB(IStateRingParticipant participant)
{
this.participant = participant;
}
}
注意,构造函数的一部分是它接受参与者。 状态管理所有实际的处理,然后在状态良好并准备就绪时,实例化下一个状态(也保持当前状态),依此类推。
使用状态模式,您可以将所有功能路由到状态,然后让他们确定何时应更改状态(通过实例化状态的新实例)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.