Responding to li...
>
> Could someone shine some light on the following:
> How can I show the state pattern in a UML sequence diagram?
The short answer is: exactly the same way you would show any other
object collaborations.
The parent State class defines a suite of actions that each subclass
implements differently. Those actions are invoked in collaboration just
like any other object's actions in an SD. One swimlane is the object
sending the message and the other is the State object receiving it.
The State pattern is described in the Class diagram as a generalization,
so one has a choice of whether the receiver swimlane is the superclass
(polymporphic dispatch) or one of the concrete subclasses (the client
only collaborates with a particular substate). This is exactly the same
as one would represent a collaboration with any generalization object.
>
> UML student question:
> My idea so far is: In the lifeline you do an self call and call the
> method to
> change it's state. Is it nessecary to show all states? Someone wanted
> display the different states as "instances" with it's own lifeline. My
> thought is: it's not an instance, it's an state. This last thing would
> be wrong I guess.
> Right?
Not exactly. In the GoF State pattern the point of changeState() is to
dynamically assign what concrete implementation should respond to
message. That essentially dynamically instantiates the relationship that
the client navigates to send a message to a state. (That's what Handle()
does in the GoF example -- it provides the right implementation for the
current solution context.) So changing the state is independent of the
individual collaborations the client has with the State's actions.
In fact, it is usually some other object entirely that invoked
changeState() to prepare the context. That interaction would also appear
on the SD as an object-to-object collaboration. However, it would be
with the Context object rather than a State object. (In the GoF
implementation example, changeState() returns a handle to the right
concrete State and the client then invokes the action through that
handle. Effectively Handle is instantiating a temporary relationship
between the client and the concrete state.)
Having said all that, my general reaction to using the GoF State pattern
to model state machines is: Don't Do That! IMO the pattern is misnamed
and and it really should be named "Role" because that is what it is most
useful for modeling. Thus one might have
<pre>
[Customer]
| *
|
| A1
|
| interacts with
| 1
[Employee] // Context
| *
|
| A2
|
| plays
| 1
[Role] // State
A
| A3
+----------+-----...
| |
[SalesClerk] [Manager]
</pre>
Note that the same employee might perform all the roles (e.g., in a
Ma&Pa store). The appropriate role and activities are determined by the
current context of interaction (sale, complaint, etc.) and that
dynamically determines how the A2 association gets instantiated (i.e.,
changeRole()).
Using the GoF State pattern to represent object states machines is a bad
idea for several reasons. The big one is that it introduces substantial
code bloat because each state must have its own class defined, which is
completely unnecessary. Aesthetically, there is a big problem because
states in state machines should not have multiple actions. [In the
underlying finite state automata, a state has a single action that
processes the incoming event message (alphabet) exactly the same way.]
There is also a minor efficiency problem because of the indirection
though Handle(). It is just an awkward and counterintuitive way to
implement object state machines that way. Finally, the primary reason
for using state machines in the first place is to support the more
general asynchronous communication model for collaborations. IOW, the
synchronous GoF model is kind of an oxymoron for state machines
--
Life is the only flaw in an otherwise perfect nonexistence
-- Schopenhauer
Rene Descartes went into a bar. The bartender asked if he would like a drink. Descartes said, "I think not," and disappeared.
H. S. Lahman
H.la...@verizon.net
software blog:
http://pathfinderpeople.blogs.com/hslahman/index.html