Lecture 9 UML Class Diagram
Lecture 9 UML Class Diagram
Ali Mirza
Sr. Assistant Professor
[email protected]
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 2
UML Class Diagrams
• Chapter 16. UML Class Diagrams
• Craig Larman
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 3
Objective
• Create design class diagrams (DCDs).
• Identify the classes, methods, and associations to show in a DCD.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 4
Class Diagrams
• The UML has notation for showing design details in
static structure.
– Class diagrams
• The definition of design class diagrams occurs within
the design phase.
– The UML does not specifically define design class diagram.
– It is a design view on SW entities, rather than an analytical view on domain
concepts.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 5
Common Class Diagram Notation
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 6
UML class diagrams in two perspectives
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 7
Influences on DCD
The creation of design class diagrams is dependent upon the prior creation of:
• Interaction diagrams
– Identifies the SW classes that participate in
the solution, plus the methods of classes.
• Conceptual model
– Adds detail to the class definitions.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 8
When to create DCDs?
• Design class diagrams & interaction diagrams
– In practice they are usually created in parallel
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 9
Classifier
• A UML classifier is "a model element that describes
behavioral and structure features”.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 10
Example of a DCD.
Draw Classes
• Draw them in a class diagram.
Add Methods/Operations
• Add method names by analyzing the interaction diagrams.
Elaborate Attributes
• Add type information to the attributes and methods.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 12
SHOWING ATTRIBUTES
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 13
Ways to Show UML Attributes: Attribute Text and
Association Lines
• Attributes of a classifier
– also called structural properties in the UML
– Shown in several ways:
• Attribute text notation, such as currentSale : Sale.
• association line notation
• both together
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 14
Attributes as Attribute Text vs. Association Line
• The full format of the Attribute Text Notation is:
– visibility name : type multiplicity = default {property-
string}
• Attribute-as-Association Line has the following style:
– a navigability arrow pointing from the source (Register) to
target (Sale) object, indicating a Register object has an
attribute of one Sale
– a multiplicity at the target end, but not the source end
– a rolename (currentSale) only at the target end to show
the attribute name
– no association name
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 15
Attribute text versus association line notation for a UML attribute
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 16
Idioms in association notation usage in different perspectives
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 17
Applying the guidelines to show attributes in two notations
• Use the attribute text notation for data type objects and
the association line notation for others.
• Both are semantically equal
• Showing an association line to another class box in the
diagram gives visual emphasis
– it catches the eye
– Emphasis on the connection between the class of objects on the
diagram.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 18
Two ways to show a collection attribute in the UML
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 19
Notes and Constraints
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 20
Note Symbols: Notes, Comments, Constraints, and Method
Bodies
• Note symbols can be used on any UML diagram
• are especially common on class diagrams
• note symbol is displayed as a dog-eared rectangle with a
dashed line to the annotated element
• A note symbol may represent several things, such as:
– a UML note or comment, which by definition have no semantic
impact
– a UML constraint, in which case it must be encased in braces '{…}‘
– a method body- the implementation of a UML operation
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 21
Constraints
• Constraints may be used on most UML diagrams, but are especially
common on class diagrams.
• A UML constraint is a restriction or condition on a UML element.
• It is visualized in text between braces;
– for example: { size >= 0 }.
• The text may be natural language or anything else,
– such as UML's formal specification language, the Object Constraint
Language (OCL).
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 22
OPERATIONS AND METHODS
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 23
Operations
• One of the compartments of the UML class box shows the
signatures of operations
• Official format of the operation
• visibility name (parameter-list) : return-type
{property-string}
• The property string contains arbitrary additional information
– e.g. exceptions that may be raised, if the operation is
abstract, and so forth
• UML allows the operation signature to be written in any
programming language
– such as Java, both expressions are possible:
• + getPlayer( name : String ) : Player {exception
IOException}
• public Player getPlayer( String name ) throws
IOException
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 24
Operation vs. Method
• An operation is not a method
• A UML operation is a declaration,
– with a name, parameters, return type, exceptions list, and possibly a set of
constraints of pre-and post-conditions.
– it isn't an implementation
• Methods are implementations
• Operation contracts in UML terms
– the definition of constraints for UML operations
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 25
Showing Methods in Class Diagrams
• Method Illustrations
– in interaction diagrams, by the details and sequence of
messages
– in class diagrams, with a UML note symbol stereotyped with
«method»
• Using UML note to show a method
– mixing static and dynamic views in the same diagram
– The method body (which defines dynamic behavior) adds a
dynamic element to the static class diagram.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 26
Operation Issues in DCDs
• The create Operation
– The create message in an interaction diagram is normally interpreted
as
• the invocation of the new operator and a constructor call in languages
such as Java and C#.
• In a DCD this create message will usually be mapped to a constructor
definition, using the rules of the language such as the constructor name
equal to the class name (Java, C#, C++, …).
• Operations to Access Attributes
– Accessing operations retrieve or set attributes, such as getPrice and
setPrice.
– These operations are often excluded (or filtered) from the class
diagram because of the high noise-to-value ratio they generate;
– for n attributes, there may be 2n uninteresting getter and setter
operations.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 27
Keywords
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 28
UML stereotypes
• UML uses stereotypes encapsulated in guillemots (« and », not << and >> )
to extend many diagram symbols
• Guillemots can be found in the latin-1 symbol set in Windows Insert
Symbol command)
• Examples: «interface» «extends» «includes» «actor» «creates»
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 29
Interfaces
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 30
Different notations to show interfaces in UML (1)
• Dependency Line Notation
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 31
Different notations to show interfaces in UML (2)
• Interface Realization
• Lollipop Notation
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 32
ASSOCIATIONS, DEPENDENCIES, GENERALIZATIONS AND
AGGREGATIONS
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 33
Association
• An association is a structural relationship that specifies that objects of one
thing are connected to objects of another.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 34
Navigation
• Navigability is a property of the role which indicates that it is possible to
navigate uni-directionally across the association from objects of the
source to target class.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 35
Dependency
• A dependency is a using relationship, specifying that a change in the
specification of one thing may affect another thing that uses it.
• Indicates that a client element (of any kind, including classes, packages,
use cases, and so on) has knowledge of another supplier element and that
a change in the supplier could affect the client.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 36
Showing Dependencies
• is-a-kind-of relationship.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 38
Aggregation
• Whole/part relationship
• Has-a relationship
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 39
Composition Over Aggregation
• Aggregation is a vague kind of association
– suggests whole-part relationships (as do many ordinary associations)
– It has no meaningful distinct semantics in the UML versus a plain association
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 40
Compositions in UML
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 41
Aggregation vs. Composition
• What relationship if any exists between the following
entities? And why? Justify! Strictly
Meaningless,
since the same
can be said using
an assocition
•An instance of Point may be part of a polygon or may be the center of a circle,
but it cannot be both
•The general rule is that, although a class may be a component of many other
classes, any instance must be a component of only one owner
•The class diagram may show multiple classes of potential owners, but any
instance has only a single object as its owner
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 42
Realization
• A realization is a semantic relationship between classifiers in which one
classifier specifies a contract that another classifier guarantees to carry
out.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 43
Qualified Association
• A qualified association has a qualifier that is used to select an object (or objects)
from a larger set of related objects, based upon the qualifier key.
• Informally, in a software perspective, it suggests looking things up by a key, such as
objects in a HashMap.
– For example, if a ProductCatalog contains many ProductDescriptions, and each one can
be selected by an itemID, then the UML notation to depict this is as follows:
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 46
The influence of interaction diagrams on class diagrams
When we draw interaction diagrams, a set of classes and their methods emerge from the creative
design process of dynamic object modeling.
From interaction diagrams the definitions of class diagrams can be generated.
Linear ordering of drawing interaction diagrams before class diagrams,
But in practice,
agile modeling practice of models in parallel,
these complementary dynamic and static views are drawn concurrently
For example, 10 minutes on one, then 10 on the other.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 47
CLASS DIAGRAMS IN UP PHASES
• Self Study
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 48
Phases
• Inception
The Design Model and DCDs will not
usually be started until elaboration because it
involves detailed design decisions, which are
premature during inception.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 49
Continue…
• Elaboration
– During this phase, DCDs will accompany the UC realization interaction
diagrams; they may be created for the most architecturally significant classes
of the design.
– CASE tools can reverse-engineer (generate) DCDs from source code.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 50
Continue…
• Construction
– DCDs will continue to be generated from the source code as an aid in
visualizing the static structure of the system.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 51
UP Artifacts and Relationships
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 52