当状态依赖于函数时,如何建模状态机? [英] How model statemachine, when state is dependent on a function?

查看:27
本文介绍了当状态依赖于函数时,如何建模状态机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个合同类.合同有效期为2010年1月1日-2010年12月31日.

I have a Contract class. The contract is valid 1 Jan 2010 - 31 Dec 2010.

它可以处于主动或被动状态,具体取决于我向实例询问其状态的日期.前任.如果我问 2010 年 7 月 4 日,它处于活动状态,但如果我问 2011 年 1 月 1 日,它处于被动状态.

It can be in state Active or Passive, depending on which date I ask the instance for it's state. ex. if I ask 4 July 2010, it's in state Active, but if I ask 1 Jan 2011, it's in state Passive.

状态机图:

从开始状态,它应该进入下一个状态?我正在考虑一个伪状态默认",其中一个入口函数决定接下来要进入哪个状态,然后是一个具有主动和被动的子状态机,然后是一个结束状态移除.

From Start state, which state should it go to next ? I'm thinking a pseudo state "default" with an entry function deciding which state to go to next, and then a substate machine with Active and Passive, and then an End state on removal.

但是对于这样一个微不足道的问题,这似乎很复杂.

But this seems to complicated for such a trivial problem.

你会怎么做?

推荐答案

嗯,你想要么做你所描述的,这将允许合同处于无效(开始/结束)状态,当有人试图在没有之前的日期事件,或者您可以尝试在构造函数中添加对该函数的调用,以便您的合约立即处于正确状态.可能还有其他解决方案,但您应该考虑合同上可能发生的其他事件(消息/方法调用)而不是日期.有些可能也会影响状态.顺便提一句.如果您不嵌入状态机,而是仅将开始和结束连接到主动和被动状态,那么您的解决方案就不会那么复杂.

Hmm, you want either do what you describe, which will allow the Contract to be in invalid (start/end) state when someone tries to use the contact without prior date event, or you can try to add the call to that function in a constructor, so your contract is in proper state right away. There might be other solutions, but you should think about what other events (messages/method calls) can happen on the contract than the date. Some might affect the stae as well. BTW. your solution is not as complicated when you instead of embedding the state machine only connects the start and end to active and passive states.

这篇关于当状态依赖于函数时,如何建模状态机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆