I have a Contract class. The contract is valid 1 Jan 2010 - 31 Dec 2010.
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.
Statemachine diagram:
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.
How would you do it ?
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.
精彩评论