【问题标题】:How State machine diagram can be represented as a Behavior for an operation in UML?状态机图如何表示为 UML 中操作的行为?
【发布时间】:2013-06-30 11:06:34
【问题描述】:

行为(方法体)可以是状态机或活动——活动很容易理解,因为它们相当于程序代码。

我不明白如何将状态机用作操作的行为?

您能提供一个简单的例子吗?

---注意---

操作是一个仅规范的元素 - 将其想象为 OO 编程语言中的方法签名。它有一个名称和一个参数列表。

行为是(除其他外)操作(或其他行为特征,如接收)在调用时所做的 - 将其想象为方法的主体。

【问题讨论】:

  • 正如它所说的那样,状态机用于表示程序的不同/可能的状态(无活动),例如对象/模块的状态,例如睡眠状态、运行、等待和更多,但您也可以在序列图中组合状态:D
  • 感谢 @user2511414 的评论,但当我阅读 UML 规范时,可以通过三种不同的机制指定行为(第 13 节常见行为):状态机、活动和交互其中哪一个应该使用三种方式,主要取决于行为的种类。
  • 我明白你的意思了,在状态机中你代表一种行为,但一般来说(例如运行状态、持久化、加载、关闭),每个状态都会包含一些活动跨度>
  • @user2511414 我更新了我的问题。这有意义吗?
  • 正如您所说,操作只是一个名称,并不能确定具体做什么,但行为是一组业务/命令共同进行操作,除了它们可以改变状态应用程序也是如此,首先我们需要了解状态在应用程序中的确切含义!?

标签: behavior uml state-machine state-diagram


【解决方案1】:

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

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

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

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

第一次。

【讨论】:

    【解决方案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.)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多