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.

user2019510
  • 1,460
  • 5
  • 16
  • 29
  • it's manes as it says, state machine is used for representing different/possible state of the program, (no activity), something like state of objects/modules, like sleeping state, running, waiting, and more, but you would combine states in sequence diagram too :D –  Jun 30 '13 at 11:11
  • thank you @user2511414 for your comment but as I read through UML specification A Behavior (section 13 Common Behavior) can be specified by means of three different mechanisms: State Machines, Activities,and Interactions Which of these three means shall be used mostly depends on the kind of behavior. – user2019510 Jun 30 '13 at 11:19
  • I see what you mean dude, in state machine you would represent a behavior, but in general (for example running state, persisting, loading, shutting down), that each state would include some activity –  Jun 30 '13 at 11:29
  • @user2511414 I updated my question. Does this make sense? – user2019510 Jun 30 '13 at 12:05
  • Exactly as you mentioned,operation is just a name, and it doesn't identify what exactly does, but the behavior are set of businesses/commands that together make a operation, beside they could change the state of the application too, first we need to know about the what does state mean exactly in an application!? –  Jun 30 '13 at 12:55
  • state means attribute field in class diagram – user2019510 Jun 30 '13 at 12:58
  • in fact a behavior isn't limit to some commands! they also would change the application's state and situation that would effect the functionality of the system, for example when an application goes in power-saving mode, every operations are running, but with lower priority and affinity, in fact state of a program would belong to a behavior or set of behaviors –  Jun 30 '13 at 12:59
  • you are right, state are defined by fields, but would changed by methods, when a thread get started, this is the run method that effects and changes the state of the thread to running, and this is because you should mentions states in sequence diagram too –  Jun 30 '13 at 13:01
  • How can I mention states in sequence diagram ? – user2019510 Jun 30 '13 at 14:07
  • of course buddy check this [link](http://s2.postimg.org/72mz4h0d5/sequencewithstatemachine.png), if there is some explain need, just feel free buddy :D –  Jun 30 '13 at 20:14
  • Hi @user2511414 ! thank you for the pointer:) I didn't see this situation anywhere o.O .Where can I find this type of sequence diagram in UML specification? – user2019510 Jun 30 '13 at 20:21
  • because I only know about state invariant placed on lifeline(for writing run-time constraint) but didn't know about state? – user2019510 Jun 30 '13 at 20:29
  • download the spec [here](http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF/) and check the page 530 (part 14.3.25), or [here](http://www.sparxsystems.com/enterprise_architect_user_guide/modeling_languages/statecontinuation.html), I suggest you UMLet, easy and fast :D –  Jun 30 '13 at 20:37
  • Thank you very much @user2511414 for clarifications and your kind heart:) Could you Please provide a short textual description of your diagram? http://s2.postimg.org/72mz4h0d5/sequencewithstatemachine.png because I didn't know how to interpret your diagram after adding states? – user2019510 Jun 30 '13 at 21:09
  • my pleasure buddy, as it says, the FinalClass goes into the waiting state by calling wait() method of lock object, simultaneously the f instance(runnable) starts its task that while it's in processing it's in running state, after f thread finishes its job, notify the lock object that causes the FinalClass thread get to the persisting state (maybe saving some data), these StateInvariant are used when state is really importnt to mention in sequence diagrams –  Jun 30 '13 at 21:21

2 Answers2

2

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

sfinnie
  • 9,854
  • 1
  • 38
  • 44
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.)
    }

}
milesma
  • 1,561
  • 1
  • 15
  • 37