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

丶灬走出姿态 提交于 2019-12-07 17:26:41

问题


Behaviors (Method Body)can be state machines or activities - activities are easy to understand, as they are the equivalent of procedural code.

I don't understand how a state machine can be used as the behavior for an operation?

Could you Please provide a simple example for that?

---Note---

Operation is a specification-only element - imagine it as the method signature in OO programming languages. It has a name and a list of parameters.

Behavior is (among other things) what an operation (or another behavioral feature such as a reception) does when invoked - imagine it as the body of the method.


回答1:


"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.




回答2:


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.)
    }

}


来源:https://stackoverflow.com/questions/17389717/how-state-machine-diagram-can-be-represented-as-a-behavior-for-an-operation-in-u

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