How State machine diagram can be represented as a Behavior for an operation in UML?

余生长醉 提交于 2019-12-06 01:46:28

"Just because you can doesn't mean you should".

In other words: it may be legal to use a state model to define an operation's behaviour - but it doesn't mean you should. I've never come across a scenario where it would have been useful; but of course that doesn't mean they don't exist. It's also symptomatic of the lack of cohesion in some of the UML specification.

It would be appropriate where the operation (not the enclosing class) had stateful behaviour. To use a really contrived example: consider a method TcpConnection.close(). If the connection was already closed, then calling close() would have no effect. If the connection was open then calling close() would close it.

[However: as an example that also illustrates why I've never found the need for a method-specific state model. The state model really belongs with the class, not the operation].

hth.

The easiest way to understand what is a Behavior: It can change your member variable's value. E.g.

class MyClass
{
    public Integer i = 0;
    public void Operation1(){
        i++; //This could be an interpretation of of opaque action from an Activity
    };
    public void RunStateMachine(){
        //You can use state's entry/doActivity/exit behavior. E.g. put "i++" in any of them
        //You can use transition's effect behavior. E.g. put "i++" in it
        //state's entry/doActivity/exit, transition's effect can specify to another behavior, e.g. run another Activity or statemachine, 
        //UML provides a good recursive way to let user to model what ever they wanted.

        //NOTE: When using statemachine as behavior, you should know that the context (e.g. MyClass my = new MyClass(); here my is the context) of the statemachine 
        //is expecting an EventOccurence if the transitions has triggers. 
        //If no triggers are defined in any of the transitions in a statemachine, it can be think of being downgraded as an activity
        // (well, it is not conforming to UML, but you can think in that way.)
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!