UML Diagrams: Class Diagram
UML Diagrams: Class Diagram
Class Diagram
The UML Class diagram is a graphical notation used to construct and visualize object-oriented systems. A
class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes
the structure of a system by showing the system's:
• classes,
• their attributes,
• operations (or methods),
• and the relationships among objects.
A Class is a blueprint for an object. Objects and classes go hand in hand. We can't talk about one without
talking about the other. And the entire point of Object-Oriented Design is not about objects, it's about classes,
because we use classes to create objects. So, a class describes what an object will be, but it isn't the object
itself.
In fact, classes describe the type of objects, while objects are usable instances of classes. Each Object was
built from the same set of blueprints and therefore contains the same components (properties and methods).
The standard meaning is that an object is an instance of a class and object - Objects have states and behaviors.
Example
• A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an
instance of a class.
UML Class Notation
A class represent a concept which encapsulates state (attributes) and behavior (operations). Each attribute
has a type. Each operation has a signature. The class name is the only mandatory information.
Class Name:
• The name of the class appears in the first partition.
Class Attributes:
• Attributes are shown in the second partition.
• The attribute type is shown after the colon.
• Attributes map onto member variables (data members) in code.
Class Operations (Methods):
• Operations are shown in the third partition. They are services the class provides.
• The return type of a method is shown after the colon at the end of the method signature.
• The return type of method parameters are shown after the colon following the parameter name.
Operations map onto class methods in code
Class Visibility
The +, - and # symbols before an attribute and operation name in a class denote the visibility of the attribute
and operation.
Class Relationships
A class may be involved in one or more relationships with other classes. A relationship can be one of the
following types: (Refer to the figure on the right for the graphical representation of relationships).
Relationship Type Graphical Representation
Simple Association:
• A structural link between two peer classes.
• There is an association between Class1 and Class2
• A solid line connecting two classes
Aggregation:
A special type of association. It represents a "part of" relationship.
• Class2 is part of Class1.
• Many instances (denoted by the *) of Class2 can be associated
with Class1.
• Objects of Class1 and Class2 have separate lifetimes.
• A solid line with an unfilled diamond at the association end
connected to the class of composite
Composition:
A special type of aggregation where parts are destroyed when the whole
is destroyed.
• Objects of Class2 live and die with Class1.
• Class2 cannot stand by itself.
• A solid line with a filled diamond at the association connected
to the class of composite
Dependency:
• Exists between two classes if the changes to the definition of
one may cause changes to the other (but not the other way
around).
• Class1 depends on Class2
• A dashed line with an open arrow
Cardinality
Relationship Names
• Names of relationships are written in the middle of the association line.
• Good relation names make sense when you read them out loud:
• "Every spreadsheet contains some number of cells",
• "an expression evaluates to a value"
• They often have a small arrowhead to show the direction in which direction to read the relationship,
e.g., expressions evaluate to values, but values do not evaluate to expressions.
Relationship - Roles
• A role is a directional purpose of an association.
• Roles are written at the ends of an association line and describe the purpose played by that class in the
relationship.
• E.g., A cell is related to an expression. The nature of the relationship is that the expression is
the formula of the cell.
Activity
Is used to represent a set of actions
Action
A task to be performed
Control Flow
Shows the sequence of execution
Object Flow
Show the flow of an object from one activity (or action) to another activity
(or action).
Initial Node
Portrays the beginning of a set of actions or activities
Object Node
Represent an object that is connected to a set of Object Flows
Decision Node
Represent a test condition to ensure that the control flow or object flow only
goes down one path
Merge Node
Bring back together different decision paths that were created using a
decision-node.
Fork Node
Split behavior into a set of parallel or concurrent flows of activities (or
actions)
Join Node
Bring back together a set of parallel or concurrent flows of activities (or
actions).
Swimlane and Partition
A way to group activities performed by the same actor on an activity
diagram or to group activities in a single thread
Characteristics of State
• State represent the conditions of objects at certain points in time.
• Objects (or Systems) can be viewed as moving from state to state
• A point in the lifecycle of a model element that satisfies some condition, where some particular action
is being performed or where some event is waited
Initial and Final States
• The initial state of a state machine diagram, known as an initial pseudo-state, is indicated with a solid
circle. A transition from this state will show the first real state
• The final state of a state machine diagram is shown as concentric circles. An open loop state machine
represents an object that may terminate before the system terminates, while a closed loop state machine
diagram does not have a final state; if it is the case, then the object lives until the entire system terminates.
Example:
Events
An event signature is described as Event-name (comma-separated-parameter-list). Events appear in the internal
transition compartment of a state or on a transition between states. An event may be one of four types:
1. Signal event - corresponding to the arrival of an asynchronous message or signal
2. Call event - corresponding to the arrival of a procedural call to an operation
3. Time event - a time event occurs after a specified time has elapsed
4. Change event - a change event occurs whenever a specified condition is met
Characteristics of Events
• Represents incidents that cause objects to transition from one state to another.
• Internal or External Events trigger some activity that changes the state of the system and of some of its
parts
• Events pass information, which is elaborated by Objects operations. Objects realize Events
• Design involves examining events in a state machine diagram and considering how those events will be
supported by system objects
Transition
Transition lines depict the movement from one state to another. Each transition line is labeled with the event that
causes the transition.
• Viewing a system as a set of states and transitions between states is very useful for describing complex
behaviors
• Understanding state transitions is part of system analysis and design
• A Transition is the movement from one state to another state
• Transitions between states occur as follows:
1. An element is in a source state
2. An event occurs
3. An action is performed
4. The element enters a target state
• Multiple transitions occur either when different events result in a state terminating or when there are
guard conditions on the transitions
• A transition without an event and action is known as automatic transitions
Actions
Action is an executable atomic computation, which includes operation calls, the creation or destruction of
another object, or the sending of a signal to an object. An action is associated with transitions and during which
an action is not interruptible - e.g., entry, exit
Activity
Activity is associated with states, which is a non-atomic or ongoing computation. Activity may run to
completion or continue indefinitely. An Activity will be terminated by an event that causes a transition from
the state in which the activity is defined
Characteristics of Action and Activities
• States can trigger actions
• States can have a second compartment that contains actions or activities performed while an entity is in
a given state
• An action is an atomic execution and therefore completes without interruption
• Five triggers for actions: On Entry, Do, On Event, On Exit, and Include
• An activity captures complex behavior that may run for a long duration - An activity may be interrupted
by events, in which case it does not complete occur when an object arrives in a state.
Simple State Machine Diagram Notation
State Machine Diagrams are often used for deriving testing cases, here is a list of possible test ideas:
• Idle state receives Too Hot event
• Idle state receives Too Cool event
• Cooling/Startup state receives Compressor Running event
• Cooling/Ready state receives Fan Running event
• Cooling/Running state receives OK event
• Cooling/Running state receives Failure event
• Failure state receives Failure Cleared event
• Heating state receives OK event
• Heating state receives Failure event
History States
Unless otherwise specified, when a transition enters a composite state, the action of the nested state machine
starts over again at the initial state (unless the transition targets a substate directly). History states allow the
state machine to re-enter the last substate that was active prior to leaving the composite state. An example
of history state usage is presented in the figure below.
Concurrent State
As mentioned above, states in state machine diagrams can be nested. Related states can be grouped together
into a single composite state. Nesting states inside others is necessary when an activity involves concurrent sub-
activities. The following state machine diagram models an auction with two concurrent substates: processing
the bid and authorizing the payment limit.
Concurrent State Machine Diagram Example - Auction Process
In this example, the state machine first entering the Auction requires a fork at the start into two separate start
threads. Each substate has an exit state to mark the end of the thread. Unless there is an abnormal exit (Canceled
or Rejected), the exit from the composite state occurs when both substates have exited.
Example State Diagram
Sequence Diagram
Sequence Diagrams are interaction diagrams that detail how operations are carried out. They capture the
interaction between objects in the context of a collaboration. Sequence Diagrams are time focus and they
show the order of the interaction visually by using the vertical axis of the diagram to represent time what
messages are sent and when.
Note That: Class and object diagrams are static model views. Interaction diagrams are dynamic. They describe
how objects collaborate.
Lifeline
• A lifeline represents an individual participant in the Interaction.
Activations
• A thin rectangle on a lifeline) represents the period during
which an element is performing an operation.
• The top and the bottom of the of the rectangle are aligned with
the initiation and the completion time respectively
Call Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Call message is a kind of message that represents an invocation
of operation of target lifeline.
Return Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Return message is a kind of message that represents the pass of
information back to the caller of a corresponded former
message.
Self Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Self message is a kind of message that represents the invocation
of message of the same lifeline.
Recursive Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Recursive message is a kind of message that represents the
invocation of message of the same lifeline. It's target points to
an activation on top of the activation where the message was
invoked from.
Create Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Create message is a kind of message that represents the
instantiation of (target) lifeline.
Destroy Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Destroy message is a kind of message that represents the
request of destroying the lifecycle of target lifeline.
Duration Message
• A message defines a particular communication between
Lifelines of an Interaction.
• Duration message shows the distance between two time instants
for a message invocation.
Note
A note (comment) gives the ability to attach various remarks to elements. A comment carries no semantic force, but
may contain information that is useful to a modeler.
Sequence Fragments
• UML 2.0 introduces sequence (or interaction) fragments. Sequence fragments make it easier to create
and maintain accurate sequence diagrams
• A sequence fragment is represented as a box, called a combined fragment, which encloses a portion of
the interactions within a sequence diagram
• The fragment operator (in the top left cornet) indicates the type of fragment
• Fragment types: ref, assert, loop, break, alt, opt, neg
Note That:
• It is possible to combine frames in order to capture, e.g., loops or branches.
• Combined fragment keywords: alt, opt, break, par, seq, strict, neg, critical, ignore, consider, assert and
loop.
• Constraints are usually used to show timing constraints on messages. They can apply to the timing of
one message or intervals between messages.
Combined Fragment Example
User requirements are captured as use cases that are refined into scenarios. A use case is a collection of
interactions between external actors and a system. In UML, a use case is:
"the specification of a sequence of actions, including variants, that a system (or entity) can perform, interacting
with actors of the system."
A scenario is one path or flow through a use case that describes a sequence of events that occurs during one
particular execution of a system which is often represented by a sequence diagram.
Sequence Diagram - Model before Code
Sequence diagrams can be somewhat close to the code level, so why not just code up that algorithm rather than
drawing it as a sequence diagram?
• A good sequence diagram is still a bit above the level of the real code
• Sequence diagrams are language neutral
• Non-coders can do sequence diagrams
• Easier to do sequence diagrams as a team
• Can be used for testing and/or UX Wireframing