0% found this document useful (0 votes)
285 views

Lecture 9 UML Class Diagram

This document provides an overview of UML class diagrams. It discusses common class diagram notation, design class diagrams, when to create them, and how to make one. Key aspects covered include classifiers like classes and interfaces, showing attributes using text or association lines, and the influence of interaction diagrams on class diagrams. The goal is to teach how to create design class diagrams that identify classes, methods, and associations based on prior interaction and conceptual models.

Uploaded by

Ossama Zulfiqar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
285 views

Lecture 9 UML Class Diagram

This document provides an overview of UML class diagrams. It discusses common class diagram notation, design class diagrams, when to create them, and how to make one. Key aspects covered include classifiers like classes and interfaces, showing attributes using text or association lines, and the influence of interaction diagrams on class diagrams. The goal is to teach how to create design class diagrams that identify classes, methods, and associations based on prior interaction and conceptual models.

Uploaded by

Ossama Zulfiqar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

LECTURE

9 UML Class Diagram


CSC 318
Object Oriented Analysis and Design
Fall 2019

Ali Mirza
Sr. Assistant Professor
[email protected]

Department of Computer Science


Bahria University, Islamabad
Contents
• UML Class Diagrams • Interfaces & Interface Example
– Common Class Diagram Notation • UML stereotypes
• Association, Navigation,
– Design Class Diagram Dependency
– UML class diagrams in two • Generalization, Aggregation &
perspectives
Composition
– When to create DCDs?
– Classifier • Realization
– How to make a DCD? • Relationship Between Interaction
and Class Diagrams
• Concrete Guidelines to Create
DCDs – The influence of interaction
diagrams on class diagrams
– Ways to Show UML Attributes:
Attribute Text and Association • Class Diagrams in UP Phases
Lines – UP Artifacts and Relationships
– Note Symbols: Notes, Comments,
Constraints, and Method Bodies

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

• Classifiers can also be specialized.

• They are a generalization of many of the elements of


the UML, including classes, interfaces, use cases, and
actors.

• In class diagrams, the two most common classifiers are


regular classes and interfaces.

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.

 A design class diagram


illustrates the
specifications for SW
classes and interfaces
 Typical information
included:
 Classes, associations,
and attributes
 Interfaces, with their
operations and
constants
 Methods
 Attribute type
information
Ali Mirza | Department of Computer Science Navigability
CSC 318– Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
 Dependencies Slide: 11
How to make a DCD?
Identify Classes
• Identify all the classes participating in the SW solution by
analyzing the interaction diagrams.

Draw Classes
• Draw them in a class diagram.

Add Attributes (From Domain Model)


• Duplicate the attributes from the associated concepts in the
domain model.

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

• Attributes as Associations in DCDs


– UML metamodel also allows multiplicity and role-names at the
association end and also an association name
• not usually useful in the context of a DCD
• Domain Model Perspective:
– When using class diagrams for a domain model
• Show association names but avoid navigation arrows
• As a domain model is not a software perspective

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

• A property string such as {ordered} or {ordered, List} is


possible.
• {ordered} is a UML-defined keyword that implies the elements
of the collection are (the suspense builds…) ordered.
• Another related keyword is {unique}, implying a set of unique
elements.

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

 A UML keyword is a textual adornment to categorize a model element.


 e.g. the keyword to categorize that a classifier box is an interface is
(shocking surprise!) «interface».
 Few sample predefined UML keywords include:[
Keyword Meaning Example Usage
«actor» classifier is an actor in class diagram, above classifier
name
«interface» classifier is an interface in class diagram, above classifier
name
{abstract} abstract element; can't be in class diagrams, after classifier
instantiated name or operation name
{ordered} a set of objects have some in class diagrams, at an
imposed order association end

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

• Socket Line Notation


– The socket notation is new to UML 2. It's useful to indicate "Class X requires
(uses) interface Y" without drawing a line pointing to interface Y

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.

• Navigability implies visibility.

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.

• Dependency can be viewed as another version of coupling, a traditional


term in software development when an element is coupled to or depends
on another.

Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 36
Showing Dependencies

• There are many kinds of dependency


– having an attribute of the supplier type
– sending a message to a supplier; the visibility to the
supplier could be:
• an attribute, a parameter variable, a local variable, a global variable, or class
visibility (invoking static or class methods)
– receiving a parameter of the supplier type
– the supplier is a superclass or interface
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 37
Generalization
• A generalization is a relationship between a general thing (called the super
class or parent) and a more specific kind of that thing (called the subclass
or child).

• 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

• Composition, also known as composite aggregation


– strong kind of whole-part aggregation
– useful to show in some models

• A composition relationship implies that


– an instance of the part (such as a Square) belongs to only one composite instance (such
as one Board) at a time,
– the part must always belong to a composite (no free-floating Fingers), and
– the composite is responsible for the creation and deletion of its parts either by itself
creating/deleting the parts, or by collaborating with other objects.
• if the composite is destroyed, its parts must either be destroyed, or attached to another
composite (no free-floating Fingers allowed!)

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.

• We use realization in two circumstances:


– In the context of interfaces.
– In the context of collaborations.

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:

The change in multiplicity.


Contrast (a) vs. (b),
qualification reduces the
multiplicity at the target
end of the association,
usually down from
many to one,
It implies the
selection of usually
one instance from a
larger set
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 44
Association Class
• An association class allows you treat an association
itself as a class,
– and model it with attributes, operations, and other
features.

 In the UML, it is illustrated with a dashed line from the


association to the association class.
Ali Mirza | Department of Computer Science CSC 318 – Object Oriented Analysis and Design
Bahria University, Islamabad 9. UML Class Diagram
Slide: 45
RELATIONSHIP BETWEEN INTERACTION
AND CLASS DIAGRAMS

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

You might also like