How can a state machine diagram be represented as a behavior for an operation in UML?

Behavior (Method body) can be state machines or actions - actions are easy to understand, since they are equivalent to procedural code.

I do not understand how a state machine can be used as a behavior for an operation?

Could you provide a simple example for this?

--- Note ---

An operation is only an element of the specification โ€” imagine that it is like a method signature in OO programming languages. It has a name and a list of options.

Behavior - in particular, is what the operation (or another behavioral function, such as a reception) does when invoked - imagine that this is the body of the method.

+3
source share
2 answers

"Just because you can doesnโ€™t mean what you need."

In other words: it may be legal to use a state model to determine the behavior of an operation, but that does not mean that you should. I have never come across a scenario where this would be useful; but, of course, this does not mean that they do not exist. It is also a symptom of lack of grip in some UML specifications.

It would be appropriate for the operation (and not the enclosing class) to have stateful behavior. To use a really far-fetched example: consider the TcpConnection.close() method. If the connection has already been closed, calling close() will have no effect. If the connection was open, calling close() will close it.

[However: as an example, which also illustrates why I never found the need for a state model for a particular method. The state model really belongs to the class, not the operation].

NTN.

+2
source

The easiest way to understand what this behavior is: it can change the value of a member variable. For instance.

 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 entry/doActivity/exit behavior. Eg put "i++" in any of them //You can use transition effect behavior. Eg put "i++" in it //state entry/doActivity/exit, transition effect can specify to another behavior, eg 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 (eg 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.) } } 
+2
source

Source: https://habr.com/ru/post/1485267/


All Articles