行为(方法主体)可以是状态机或活动-活动很容易理解,因为它们相当于过程代码。
我不明白状态机如何被用作操作的行为
你能提供一个简单的例子吗
---注---
操作是一个仅限规范的元素——将其想象成OO编程语言中的方法签名。它有一个名称和一个参数列表。
行为(除其他外)是一个操作(或另一个行为特征,如接收)在被调用时所做的事情——将其想象为方法的主体。
"仅仅因为你可以并不意味着你应该"。
换句话说:使用州模型来定义运营的行为可能是合法的,但这并不意味着你应该这样做。我从来没有遇到过有用的场景;当然,这并不意味着它们不存在。它也是一些UML规范中缺乏内聚性的症状。
在操作(而不是封闭类)具有有状态行为的情况下,这将是合适的。使用一个非常做作的例子:考虑一个方法TcpConnection.close()
。如果连接已经关闭,那么调用close()
将无效。如果连接是打开的,那么调用close()
将关闭它
[然而:作为一个例子,这也说明了为什么我从未发现需要特定于方法的状态模型。状态模型实际上属于类,而不是操作]。
hth。
理解什么是Behavior的最简单方法:它可以更改成员变量的值。例如
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.)
}
}