Chapter 11 - Dynamic-Object-Modeling
Chapter 11 - Dynamic-Object-Modeling
No matter what happens, someone will find a way to take it too seriously.
319
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
320 DYNAMIC OBJECT MODELING
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.1 UML Sequence Diagrams 321
UML sequence diagrams model the flow of logic within your system in a
visual manner, enabling you both to document and validate your logic, and
are commonly used for both analysis and design purposes. In fact they are
often described as the bridging technique between analysis artifacts such as
system use cases and robustness diagrams and design-oriented artifacts such
as UML class diagrams. Sequence diagrams are the most popular UML artifact
for dynamic modeling. Sequence diagrams, along with class diagrams and
physical data models (Chapter 12), are in my opinion the most important
design-level models for modern business application development.
Sequence diagrams are typically used to model the following:
r Usage scenarios. A usage scenario is a description of a potential way your
system is used. The logic of a usage scenario may be part of a use case,
perhaps an alternate course. It may also be one entire pass through a use
case, such as the logic described by the basic course of action or a portion
of the basic course of action, plus one or more alternate scenarios. The
logic of a usage scenario may also be a pass through the logic contained in
several use cases. For example, a student enrolls in the university, and then
immediately enrolls in three seminars.
r The logic of methods. Sequence diagrams can be used to explore the logic
of a complex operation, function, or procedure. One way to think of se-
quence diagrams, particularly highly detailed diagrams, is as visual object
code.
r The logic of services. A service is effectively a high-level method, often one
that can be invoked by a wide variety of clients. This includes Web services
as well as business transactions implemented by a variety of technologies
such as CICS/COBOL or CORBA-compliant object request brokers (ORBs).
Let us start with three simple examples. Figure 11.1 depicts a UML sequence
diagram for the Enroll in University use case of Fig. 11.2, taking a system-level
approach where the interactions among the actors and the system are shown.
Figure 11.3 depicts a sequence diagram for the detailed logic of a service to
determine whether an applicant is already a student at the university. Figure
11.4 shows the logic for how to enroll in a seminar. I will often develop a
system-level sequence diagram with my stakeholders to help to both visualize
and validate the logic of a usage scenario. It also helps me to identify significant
methods/services, such as checking to see whether the applicant already exists
as a student, which my system must support.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
322 DYNAMIC OBJECT MODELING
FIGURE 11.1. A high-level system sequence diagram for Enroll in University use case.
Identifier: UC 19
Description:
Enroll someone in the university.
Preconditions:
The Registrar is logged into the system.
The Applicant has already undergone initial checks to verify that they are
eligible to enroll.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.1 UML Sequence Diagrams 323
Postconditions:
The Applicant will be enrolled in the university as a student if they are eligible.
Alternate Course A: . . . .
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
324 DYNAMIC OBJECT MODELING
The reason they are called sequence diagrams should be obvious: the se-
quential nature of the logic is shown via the ordering of the messages (the
horizontal arrows). The first message starts in the top left corner, the next
message appears just below that one, and so on.
The boxes across the top of the diagram represent classifiers or their in-
stances, typically use cases, objects, classes, or actors. Because you can send
messages to both objects and classes, objects respond to messages through the
enrollStudent(aStudent)
isStudentEligible(aStudent)
getSeminarHistory()
seminarHistory
eligibilityStatus
enrollmentStatus
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.1 UML Sequence Diagrams 325
TIP
Design Is So Important You Should Do It Every Day
It is critical to think through how you are going to build something, to actually
design it, before you build it. Your design efforts may take the form of a sketch
on a whiteboard, a detailed model created with a sophisticated modeling tool,
or a simple test that you write before you write business code. Agile developers
realize that design is so important that they do it every day, that design is not
just a phase that you do early in the project before getting to the “real work” of
writing the source code.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
326
Enroll In Seminar
Basic Course of
Action
A Student :MainMenu :SecurityLogon :SeminarSelector
SD#: UC17-01 <<actor>> <<UI>> <<UI>> <<UI>>
:EnrollInSeminar
<<controller>>
1. Student indicates wish to enroll wish to enroll
<<create>>
2. Student inputs name and number name
student number
FIGURE 11.5. A UML sequence diagram for the basic course of action for the Enroll in Semi-
nar use case.
327
schedule
:FeeDisplay Student seminar:Seminar theStudent :StudentFees
:StudentSchedule
<<UI>> :Student
isEligible(name, studentNumber)
<<create>>
Note: Need to
flesh this
message
out more.
isEligibleToEnroll(theStudent) qualifications()
getSchedule()
determineFit(seminar)
calculateFees(seminar, theStudent)
<<create>>
verification
X enrollStudent(theStudent)
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.1 UML Sequence Diagrams 329
Enroll In Seminar
Alternate Course of
Action: Student Does
not Have Prerequisites :EnrollInSeminar :IneligibilityNotice :SeminarDetails seminar:Seminar
<<controller>> <<UI>> <<UI>>
SD #: UC17-01C Student
chooses
seminar isEligibleToEnroll(theStudent): false
C.6. System determines ineligibility to enroll
<<create>>
C.7. System informs the student of ineligibility
C.8. System informs the student of <<create>>
prerequisites
C.9. Use case resumes at step 4
TIP
Design Models Need to Be Just Barely Good Enough
You do not need to model every single detail in your models, and the models do
not need to be perfect.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
330 DYNAMIC OBJECT MODELING
Student
new
studentPage:
: TranscriptBuilder student: : Seminar <<system>>
StudentInfoPage
Student Printer
new(student)
getSeminars()
calculateMark()
Print
print(studentPage)
all artifacts that I intend to keep). Notes are depicted as a piece of paper with
the top-right corner folded over. I also used a note to indicate future work
that needs to be done, during either analysis or design, in this diagram—
the qualifications() message likely represents a series of messages sent to the
student object. Common UML practice is to anchor a note to another model
element with a dashed line when appropriate; in this case the note is attached
to the message.
Although Fig. 11.5 models the logic of the basic course of action for the En-
roll in Seminar use case, how would you go about modeling alternate courses?
The easiest way to do so is to create a single sequence diagram for each alter-
nate course, as you see depicted in Fig. 11.6. This diagram models only the
logic of the alternate course, as you can tell by the numbering of the steps
on the left-hand side of the diagram, and the header note for the diagram
indicates it is an alternate course of action. Also notice how the ID of this
diagram includes that this is alternate course C, yet another modeling rule of
thumb I have found useful over the years.
Let us consider other sequence diagramming notation. Figure 11.6 includes
an initial message, Student chooses seminar, which is indicated by the filled in
circle. This could easily have been indicated via a method invocation, perhaps
enrollIn(seminar). Figure 11.7 shows another way to indicate object creation—
sending the new message to a class. We have actually seen three ways to achieve
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.1 UML Sequence Diagrams 331
this, the other two being to send a message with the <<create>> stereotype
and/or to send a message into the side of the classifier symbol (for example,
in Fig. 11.5 the message going into the side of EnrollInSeminar or in Fig. 11.7
the message going into the side of StudentInfoPage). My advice is to choose
one style and stick to it.
Figure 11.7 and Fig. 11.8 each depict a way to indicate looping logic.
One way is to show a frame with the label loop and a constraint indicating
what is being looped through, such as for each seminar in Fig. 11.7. Another
approach is to simply precede a message that will be invoked several times
with an asterisk, as you see in Fig. 11.8 with the inclusion of the Enroll in
Seminar use case.
Figure 11.7 includes an asynchronous message, the message to the system
printer that has the partial arrowhead. An asynchronous message is one where
the sender does not wait for the result of the message; instead it processes the
result when and if it ever comes back. Up until this point all other messages
have been synchronous, messages where the sender waits for the result before
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
332 DYNAMIC OBJECT MODELING
Earlier I stated that sequence diagrams are effectively a form of visual coding,
or perhaps another way to think of it is that sequence diagrams can be used
for very detailed design. When I developed the sequence diagram of Fig.
11.5 I made several decisions that could potentially affect my other models.
For example, as I modeled Step 10, I made the design decision that the fee
display screen also handled the verification by the student that the fees were
acceptable.
Also, as I was modeling Steps 2 and 3, I came to the realization that stu-
dents should probably have passwords to get into the system. When you are
TIP
Design for Your Implementation Environment Judiciously
Take advantage of features of your implementation environment, but be smart
about it. Trade-offs are normal, but understand the implications and manage
the risks involved. Every time you take advantage of a unique performance
enhancement in a product (such as a database, operating system, or middleware
tool) you are likely coupling your system to that product and, thus, reducing its
portability. To minimize the impact of your implementation environment on your
systems, you can layer your software (Chapter 10) and wrap specific features
to make them appear general to their users. Wrapping is discussed in detail in
Building Object Applications That Work (Ambler 1998a).
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.1 UML Sequence Diagrams 333
I have been trying to explain to people how to draw sequence diagrams for
years, and what I have discovered is that the people who get it are either
very good at thinking in a logical manner and/or they are good at writing
software code. Sequence diagramming really is visual coding, even when you
are modeling a usage scenario via a system-level sequence diagram.
When I am creating a sequence diagram I will start by identifying the scope
of what I am trying to model, and because I prefer to follow the AM practice
model in small increments I will typically tackle small usage scenarios at the
system level or a single method/service at the detailed object level. A diagram
such as Fig. 11.5 is too complex to be useful in my experience.
I will then work through the logic with at least one more person, laying out
classifiers across the top as I need them. I automatically add the object lifelines
but, as I indicated earlier, will typically not invest time adding activation boxes.
The heart of the diagram is in the messages, which I add to the diagram one
at a time as I work through the logic. I rarely indicate return values; instead I
will give messages intelligent names, which often make it clear what is being
returned.
It is interesting to note that as you sequence diagram you will identify new
responsibilities for classes and objects, and, sometimes, even new classes. The
implication is that you may want to update your class model appropriately;
agile modelers will follow the practice create several models in parallel, some-
thing that CASE tools will do automatically. Remember that each message
sent to a class invokes a static method/operation on that class; each message
sent to an object invokes an operation on that object.
Regarding style issues for sequence diagramming, I prefer to draw messages
going from left to right and return values from right to left, although that
does not always work with complex objects/classes. I justify the label on
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
334 DYNAMIC OBJECT MODELING
messages and return values, so they are closest to the arrowhead. I also prefer
to layer the sequence diagrams: from left to right. I indicate the actors, then
the controller class(es), and then the user interface class(es), and, finally,
the business class(es). During design, you probably need to add system and
persistence classes, which I usually put on the right-most side of sequence
diagrams. Laying your sequence diagrams in this manner often makes them
easier to read and also makes it easier to find layering logic problems, such as
user-interface classes directly accessing persistence classes.
The most important thing that you can do is to keep your diagrams simple,
both content wise and tool wise. I will sketch sequence diagrams on white-
boards to think something through, either to verify the logic in a use case or
to design a method or service. I rarely keep sequence diagrams as I find their
true value is in their creation.
A common mistake is to try to create a complete set of sequence diagrams for
your system. I have seen project teams waste months creating several sequence
diagrams for each of their use cases, one for the basic course of action and
one for each alternate course. My advice is to only create a sequence diagram
when you have complex logic that you want to think through—if the logic is
straightforward the sequence diagram will not add any value; you had might
as well go straight to code.
A fundamental concept of the UML is that you use different diagrams for
different purposes. Class diagrams are used to model the static nature of
your system, sequence diagrams are used to model sequential logic, and state
machine diagrams are used to model the behavior of complex classes. But what
happens when you need to show the behavior of several objects collaborating
together to fulfill a common purpose? This is what UML communication
diagrams, formerly known as collaboration diagrams in UML 1.x, can be used
for because they provide a bird’s-eye view of a collection of collaborating
objects.
Communication diagrams show the message flow between objects in an
OO application and also imply the basic associations (relationships) between
classes. Figure 11.9 presents a simplified collaboration diagram for displaying
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.2 UML Communication Diagrams 335
1: getName(): seminarName
2: getDescription()
3: getLocation() 1.1: getName(): string
4: getSeatsLeft() 1.2: getNumber(): string
:Seminar 5: getStudentList() 2.1: getDescription(): string
Details :Seminar :Course
<<UI>>
* 5.1: getInfo
enrollment
:Enrollment
Actually a series of
getter invocations.
5.1.1: getInfo
student
:Student
5.1.1.1: getFullName()
a seminar details screen or page. The rectangles represent the various ob-
jects involved that make up the application. The lines between the classes
represent the relationships (associations, composition, dependencies, or in-
heritance) between them. The same notation for classes and objects used on
UML sequence diagrams are used on UML communication diagrams, another
example of the consistency of the UML. The details of your associations, such
as their multiplicities, are not modeled because this information is contained
on your UML class diagrams: remember that each UML diagram has its own
specific purpose and no single diagram is sufficient on its own. Messages are
depicted as a labeled arrow that indicates the direction of the message, using
a notation similar to that used on sequence diagrams.
Figure 11.10 summarizes the basic notation for modeling messages on
communication diagrams. Optionally, you may indicate the sequence number
in which the message is sent, indicate an optional return value, or indicate
the method name and the parameters (if any) passed to it. Sequence numbers
should be in the format A.B.C.D to indicate the order in which the messages
where sent. In Fig. 11.9 message 1 is sent to the Seminar object, which in turn
sends messages 1.1 and then 1.2 to the Course object. Message 5 is sent to the
Seminar object, which sends message 5.1 to enrollment, which in turn sends
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
336 DYNAMIC OBJECT MODELING
FIGURE 11.10. The basic notation for invoking a message on a communication diagram.
message 5.1.1 to student, and it finally sends message 5.1.1.1 to itself. Notice
how a recursive connection, or a self connection, is required so that student
can do this.
Although Fig. 11.9 applies sequence numbers to the messages, my expe-
rience is if you feel the need to use sequence numbers on communication
diagrams this is a good indication you should be using sequence diagrams in-
stead. The main difference between communication diagrams and sequence
diagrams is that sequence diagrams are good at showing sequential logic but
not that good at giving you a “big picture view” whereas communication
diagrams are the exact opposite.
In Fig. 11.9, you see the Seminar Details user-interface object collaborates
with the seminar object to obtain the information needed to display its infor-
mation. It first invokes the getter method to obtain the name of the seminar. To
fulfill this responsibility, the seminar object then collaborates with the course
object that describes it to obtain the name of the course. In this example I
showed return values for some messages but not others to provide examples
of how to do it. I will either indicate the type of the return value, for example,
string, or the result, such as seminarName. Normally I would not show return
values on this diagram because the messages are named well—my heuristic
is to only model return values when it is not clear what the message returns.
Better yet I try to find a new name for the message (remember that messages
map to operations implemented by your classes).
Another trick I often use is to consolidate trivial messages such as getter
invocations. In Fig. 11.9 I modeled the series of getter method invocations
TIP
You Typically Only Need a Few Models
Any given project team will only require a subset of the models described in this
book. Think of it like this: in your toolbox at home you have a wide array of
screwdrivers, wrenches, pliers, and so on. For any given repair job you will use
only a few of the tools. Different jobs, different tools. You never need all of your
tools at once, but over time you will use them in a variety of manners.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.3 UML State Machine Diagrams 337
Objects have both behavior and state or, in other words, they do things and
they know things. Some objects do and know more things, or at least more
complicated things, than other objects. Some objects are incredibly compli-
cated, so complex that developers can have difficulty understanding them.
To understand complex classes better, particularly those that act in different
manners depending on their state, you should develop one or more UML
state machine diagrams, formerly called state chart diagrams in UML 1.x,
describing how their instances work.
UML state machine diagrams depict the various states that an object may
be in and the transitions between those states. In fact, in other modeling
languages, it is common for this type of a diagram to be called a state-transition
diagram or even simply a state diagram. A state represents a stage in the
behavior pattern of an object, and like UML activity diagrams it is possible to
have initial states and final states. An initial state, also called a creation state,
is the one that an object is in when it is first created, whereas a final state
is one in which no transitions lead out of. A transition is a progression from
one state to another and will be triggered by an event that is either internal
or external to the object.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
338
student enrolled
[seat available]/
addStudent() Open For Enrollment
scheduled open cancelled
Proposed Scheduled
entry/ logSize()
closed
cancelled cancelled student enrolled
[no seat available] /
addToWaitingList()
seat available
Full Closed to Enrollment
seminar split enroll student / closed
addToWaitingList(); entry/ notifyInstructor()
student dropped considerSplit()
[no seat available] student dropped cancelled
[seat available] /
cancelled
enrolFromWaitingList()
FIGURE 11.11. A UML 2 state machine diagram for the Seminar class during registration.
Figure 11.11 presents an example state machine diagram for the Seminar
class during registration. The rounded rectangles represent states: you see
that instances of Seminar can be in the Proposed, Scheduled, Open For Enroll-
ment, Full, and Closed to Enrollment states. An object starts in an initial state,
represented by the closed circle, and can end up in a final state, represented
by the bordered circle. This is the exact same notation used by UML activity
diagrams, a perfect example of the consistency of the UML.
The arrows in Fig. 11.11 represent transitions, progressions from one state
to another. For example, when a seminar is in the Scheduled state, it can be
either opened for enrollment or cancelled. Figure 11.12 depicts the nota-
tion for the labels on transitions. It is mandatory to indicate the event that
causes the transition, such as open or cancelled. Guard, conditions that must
be true for the transition to be triggered, are optionally indicated. The [no seat
available] guard is shown on the student enrolled transition from the Open For
Enrollment to the Closed To Enrollment state. Guards can be described in any
manner, including both free-form text or formal language—when I am white-
boarding I will use free-form text to ensure that it is readable by everyone,
but with a sophisticated CASE tool I would consider using either a program-
ming language such as Java or a modeling language such as object constraint
language (OCL) if the tool has the ability to actually process that information
into something useful (such as executable code). The invocation of methods,
such as addToWaitingList(), can optionally be indicated on transitions. The
order in the listing implying the order in which they are invoked.
States are represented by the values of the attributes of an object. For
example, a seminar is in the Open For Enrollment state when it has been
flagged as open and seats are available to be filled. It is possible to indicate the
invocation of methods within a state; for example, upon entry into the Closed
To Enrollment state the method notifyInstructor() is invoked. The notation used
within is the same as that used on transitions, the only difference being that
the method list is mandatory and the event is optional. For example in the
Full state the operations addToWaitingList() and considerSplit() are invoked
whenever a student is enrolled. Had there been no event indicated those
methods would be invoked continuously (in a loop) whenever the object is
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
340 DYNAMIC OBJECT MODELING
TIP
Agile Designs Are Emergent, They Are Not Defined Up Front
Although you will do some modeling during “cycle 0” (just enough to get your
team going), you do not need to get a fully documented set of models in place
before you may begin coding. Your overall system design will emerge over time,
evolving to fulfill new requirements and take advantage of new technologies as
appropriate.
in that state. I indicate the methods to run during the state when I want to
indicate a method is to be run continuously: perhaps a method that polls
other objects for information or a method that implements the logic of an
important business process. Methods to be invoked when the object enters
the state are indicated by the keyword entry, as you see with both the Open
For Enrollment and Closed To Enrollment states in Fig. 11.11. Methods to be
invoked as the object exits the state are indicated by the keyword exit. The
capability to indicate method invocations when you enter and exit a state is
useful because it enables you to avoid documenting the same method several
times on each of the transitions that enter or exit the state, respectively.
Transitions are the result of the invocation of a method that causes an
important change in state. Understanding that not all method invocations
will result in transitions is important. For example, the invocation of a getter
method likely would not cause a transition because it is not changing the
state of the object (unless lazy initialization is being applied). Furthermore,
Fig. 11.11 indicates an attempt to enroll a student in a full seminar may not
result in the object changing state, unless it is determined that the seminar
should be split, even though the state of the object changes (another student
is added to the waiting list). You can see that transitions are a reflection of
your business rules. For example, you see that you can attempt to enroll a
student in a course only when it is open for enrollment or full, and that a
seminar may be split (presumably into two seminars) when the waiting list is
long enough to justify the split. You can have recursive transitions, also called
self transitions, that start and end in the same state. An example of which is
the student dropped transition when the seminar is full.
For the sake of convention, we say an object is always in one and only one
state, implying transitions are instantaneous. Although we know this is not
completely true (every method is going to take some time to run), this makes
life a lot easier for us to assume transitions take no time to complete.
Because the lifecycle of a seminar is so complex Fig. 11.11 only depicts
part of it. Figure 11.13 depicts the entire lifecycle, with Fig. 11.11 shown as
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.3 UML State Machine Diagrams 341
Enrollment
Open For
Proposed
Enrollment
student dropped
[seminar size > 0]
classes
Closed to Being end Final
Scheduled Full
Enrollment term Taught Exams
started
student dropped closed
cancelled [seminar size = 0]
a substate of the Enrollment state. I could have included all of the details in
Fig. 11.13 but chose not to in order to keep the diagram simple—I prefer to
follow the AM practices depict models simply and model in small increments.
In fact, instead of creating a diagram such as Fig. 11.13 I typically prefer
something more along the lines of the high-level view of Fig. 11.14 with
detailed views such as Fig. 11.11. This approach keeps the diagrams small
and easy to understand.
Figure 11.15 depicts a slightly different take on state machine diagrams;
this time it is much closer to an analysis level diagram because it shows what
is happening to the seminar while it is in this state from the point of view of
the people involved. It is organized into two parallel swim lanes representing
concurrent substates—one from the point of view of the professor teaching
the seminar and the other showing the actions of the teaching assistant re-
sponsible for keeping the seminar material up to date. Concurrent substates
are common with hardware but very uncommon in business classes, hence
the goofy example. Normally I would depict this sort of information using
term classes
started Being end Final
Enrollment
Taught Exams
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
342 DYNAMIC OBJECT MODELING
either a UML activity diagram or a UML timing diagram (Section 11.4) but I
needed an example to show extra notation.
Figure 11.15 shows several ways to depict transitions. The Break Starts tran-
sition exiting from the Being Taught states is applicable to all of the substates;
you know this because it exits from the superstate instead of an individual
substate. The Work Submitted transition is potentially triggered by several
sources; you know this because it is attached to the outside edge of the super-
state, whereas the source of the Break ends transition is explicitly defined as
the School Break state. The initial transition into this state is the Term Started
transition, indicated through the use of an initial state symbol. I could also
have modeled this state coming from an Enrollment state; either approach is
fair.
The Term Started and Break Ends transitions are first merged, then they lead
to a fork, which in turn leads to one or the other set of concurrent substates.
A history pseudo-state is shown (the circle with the H), indicating that if
Seminar was previously in this state, left it, and the returns that it will go back
to the substate it was originally in. The arrow leaving the history pseudo-state
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.3 UML State Machine Diagrams 343
TIP
State Modeling Often Reveals Potential Error Conditions
Identifying potential error conditions while you are state machine modeling is
common because you are constantly asking “should this transition be allowed
when the object is in this state?” When the answer is yes, you need to add the
transition to your diagram. When the answer is no, you may need to document
this potential issue so your programmers develop the proper error checking code,
so the transition is not allowed to occur.
indicates that the Deliver Course Material substate is the default the very first
time that Seminar enters the Begin Taught superstate.
When drawing a state machine diagram the thing you want to do is to
identify the creation state and whether any final states exist. After you have
done this, ask yourself what other states or stages in the life of an object does
it pass through? You can often find states by looking at the boundary values
of your attributes. For example, when the number of students in a seminar
reaches the maximum, it becomes full. Full is a valid state because different
rules now apply: when a student tries to enroll, he is put on a waiting list and
the seminar is a candidate to be split in two.
Once you have identified as many states as you can, start looking for transi-
tions. For each state, ask yourself how the object can get out of it, if possible.
This will give you a transition. Because all transitions lead from one state
to another, ask yourself what new state the transition leads you to (do not
forget about recursive transitions that lead to the same state). You should also
look at the methods you identified in your class diagram. Some of them will
correspond to a transition in your state diagram.
Although being able to inherit state diagrams would be nice, it is extremely
unlikely this will happen. The definition of inheritance says that although the
subclass is similar to the superclass, it is still different. The behavior of the
subclass is usually different than that of the superclass. This means you need
to reconsider the state diagram when you inherit from a class with one. The
one good thing is many of the states and transitions are reusable. You will
probably find either you add new states and transitions or you will redefine
some.
State machine modeling is a dynamic modeling technique, one that focuses
on identifying the behavior within your system—in this case, behavior specific
to the instances of a single class. My style is to draw one or more state machine
diagrams when a class exhibits different behavior depending on its state. For
example, the Address class is fairly simple, representing data you will display
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
344 DYNAMIC OBJECT MODELING
and manipulate in your system. Seminar objects, on the other hand, are fairly
complex, and therefore it makes sense to create a state machine diagram for
them.
In business applications it seems that a very small proportion of classes,
perhaps 5 percent at most, are complex enough to warrant the creation of
a state machine diagram. However, state machine diagrams are much more
common in real-time systems (Douglass 1999).
Timing diagrams are one of the new artifacts added to UML 2, although they
have been one of the core diagrams within the hardware design and real-time
communities for decades. They are used to explore the behaviors of one or
more objects throughout a given time period. There are two basic flavors of
timing diagram, the concise notation depicted in Fig. 11.16 and the robust
notation depicted in Fig. 11.17. Timing diagrams are often used to design
embedded software, such as control software for fuel injection system in an
automobile, although they occasionally have their uses for business software
too.
Figure 11.16 depicts the lifecycle of a single seminar, showing its timeline
quite clearly. The :Seminar label indicates that the lifeline being explored is
that of an instance of Seminar. The critical states that the seminar exhibits—
Proposed, Scheduled, Enrolling Students, Being Taught, Final Exams, Closed—are
listed across the diagram. The two lines surrounding the states are called a
general value lifeline. In this case I am using them to show the value of the
state of a seminar, but you could also explore the value of a single attribute of
an object if you so choose. When the two lines cross one another it indicates
a transition point between states. Along the bottom of the diagram timing
constraints are shown, in this case indicating the period of time during which
the seminar is in each state.
FIGURE 11.16. UML timing diagram (concise notation) for the lifecycle of a seminar.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.4 UML Timing Diagrams 345
FIGURE 11.17. UML timing diagram (robust notation) for teaching a seminar.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
346 DYNAMIC OBJECT MODELING
TIP
Prove It with Code
Never assume your design works; instead, obtain concrete feedback by writing
code that reflects your design as soon as possible.
although had the transitions been continuous in nature (such as the change
in temperature) a curvy line would have been drawn. The states/conditions
applicable to the lifeline, such as Mark Student Work and Idle, are listed along
the left-hand side of the diagram. Events/stimuli, such as Holiday Break and
Break Ends, are optionally labeled at transition points to indicate the rea-
son for the change. The arrows between timelines are messages between the
objects.
Several ways to indicate time are shown in Fig. 11.17. A timing constraint,
{Oct 5..Oct 10} is shown as are time observations (T=Nov 25 and T=Dec
1). Timing constraints and time observations can be applied to a variety of
UML diagrams, including all forms of interaction diagrams such as sequence
diagrams and communication diagrams, although I find them most useful on
timing diagrams. Unique to timing diagrams are timing rulers, depicted as
tick mark values along the bottom of the diagram.
Both notations have their purposes, although the robust notation seems
the more useful of the two particularly for objects that move back and forth
between states. The more lifelines you try to model at once the harder it is to
draw the diagram so two lifelines is about as complex as I like to get. I draw
timing diagrams on whiteboards; I do not know of any mainstream modeling
tool oriented towards business application development that supports this
kind of diagram.
In my opinion very few business application developers will find that they
need UML timing diagrams, so I really cannot recommend that you invest the
time to learn them. For the few times that you need to explore timing issues
free-form diagrams will very likely suffice.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.5 UML Interaction Overview Diagrams 347
are two types of frame shown: interaction frames, which depict any type of
UML interaction diagram (sequence diagram, communication diagram, tim-
ing diagram, interaction overview diagram), or interaction occurrence frames,
which indicate an activity or operation to invoke. There are two interaction
frames in the diagram, one that depicts a sequence diagram for determining
whether a student is eligible to enroll in a seminar and a communication
diagram to determine whether a seat is available in a seminar. These frames
indicate the type of diagram (sd for sequence diagrams, cd for communication
diagrams, td for timing diagrams, and iod for interaction overview diagrams)
and optionally the name of the diagram. The interaction occurrence frames
are of type ref and typically are anonymous as the name of the activity or
operation to be invoked should make it clear what is happening (otherwise
you need to rethink your naming strategy).
The other notation used on the diagram should be familiar to you. Decision
points are shown as diamonds, exactly as on UML activity diagrams. There
should be guards on all of the exiting flows although as you can see I will
forgo labeling some guards when it is obvious what is meant. Remember that
agile models do not need to be perfect, they just need to be good enough, but
just barely! Duration constraints, such as {0..7 msec}, are shown using the
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
348 DYNAMIC OBJECT MODELING
TIP
Continue to Use the Terminology of Your Stakeholders
It was a good idea for your requirements/analysis modeling efforts and it is a
good idea for your design modeling efforts as well.
same notation as on other types of interaction diagrams. The start and end
points use the same notation as initial and end states on UML state machine
diagrams and UML activity diagrams.
Although interaction overview diagrams are an interesting concept I doubt
that they will be used much in practice. Interaction frames are virtually useless
due to a lack of space—the diagrams that you can depict within the frames
will be too small to be of value. My suspicion is that interaction overview
diagrams will be abandoned within the marketplace in favor of UML activity
diagrams because they do not work well on whiteboards and the CASE tool
vendors can simply allow you to use other diagrams to describe the details of
activities. Perhaps I am wrong about this. Time will tell.
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
11.6 UML Composite Structure Diagrams 349
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014
350 DYNAMIC OBJECT MODELING
It is interesting to note that UML composite structure diagrams are very sim-
ilar to object role model (ORM) diagrams (Chapter 8) in notation. Although
the two diagrams explore similar issues, i.e., structure, they do so in differ-
ent ways. ORM diagrams are very good for explored detailed relationships
between entities whereas the focus of composite structure diagrams is on
exploring collaborations between entities.
To be honest, I do not find composite structure diagrams to be of much
use either. I would much rather use UML sequence diagrams for exploring a
collaboration because the notation is much more robust and because far more
developers understand the notation.
1. Develop a state machine diagram for a bank account, based on the infor-
mation in the bank case study in Chapter 1.
2. Using a UML sequence diagram, model the act of successfully transferring
funds from a checking account to a savings account using an automated
teller machine.
3. Model the usage scenario from Question 2 using a UML collaboration
diagram.
4. Model the usage scenario from Question 2 using the detailed form of a
UML timing diagram.
5. Model the usage scenario from Question 2 using a UML composite structure
diagram.
6. Compare and contrast the diagrams of Questions 2 through 5. What are
the advantages and disadvantages of each, and when would you use each?
Why?
Downloaded from https:/www.cambridge.org/core. New York University Libraries, on 20 Jan 2017 at 17:08:24, subject to the Cambridge Core terms of use, available at
Cambridge Books Online © Cambridge University Press, 2010
https:/www.cambridge.org/core/terms. https://doi.org/10.1017/CBO9780511584077.014