3

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.

4

2 回答 2

2

“仅仅因为你可以并不意味着你应该”。

换句话说:使用状态模型来定义操作的行为可能是合法的——但这并不意味着你应该这样做。我从来没有遇到过有用的场景。但这当然并不意味着它们不存在。这也是一些 UML 规范缺乏凝聚力的表现。

在操作(不是封闭类)具有状态行为的情况下是合适的。举一个非常人为的例子:考虑一个方法TcpConnection.close()。如果连接已经关闭,则调用close()将无效。如果连接是打开的,那么调用close()将关闭它。

[然而:作为一个例子,它也说明了为什么我从来没有发现需要一个特定于方法的状态模型。状态模型真正属于类,而不是操作]。

hth。

于 2013-07-03T08:30:38.020 回答
2

理解什么是行为的最简单方法:它可以改变你的成员变量的值。例如

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

}
于 2013-07-04T23:02:45.120 回答