Lect 4
Lect 4
Lecture 4
Introduction to Modelling using UML (Class Models)
1
1
TOPICS COVERED Object Oriented “OO” Characteristics
Identity
• Modelling Classes and Objects
Classification
• Some UML Diagrams
Abstraction
• UML Tools Encapsulation
• Static Diagrams Inheritance
How to use Inheritance?
• Class Model Polymorphism
• Objects Generics
Class Model
• Values and Attributes Objects
• Values and Attributes
2
4
SOME UML DIAGRAMS
Functional diagrams
• Describe the functionality of the system from the user’s point of view. It describes the
interactions between the user and the system. It includes use case diagrams.
Static diagram
• Describe the static structure of the system: Classes, Objects, attributes, associations.
Dynamic diagrams:
• Interaction diagrams
Describe the interaction between objects of the system
• State diagrams
Describe the temporal or behavioral aspect of an individual object
• Activity diagrams 5
Describe the dynamic behavior of a system, in particular the workflow.
STATIC DIAGRAMS
• Package diagrams :shows how the various classes are grouped into
packages to simplify complex class diagrams.
6
UML TOOLS
• Some UML tools can generate code once UML diagram is completed.
• Some UML tools are sketching tools.
• Rational Rose is one of the most popular software for UML creation (IBM).
• Bouml is an open source s/w. It supports python, C++, java.
• Visio is a sketching tool.
7
What is OO Software?
8
IDENTITY
•Object identity is the property by which each object can be identified and
treated as a distinct software entity.
•Each object has unique identity which distinguishes it from all its fellow
objects. It is its memory address (or handle) that is referred to by one or more
object identifiers (variables). 6
IDENTITY
8
CLASSIFICATION
•Classification means that objects with the same data structure (attributes)
and behavior (operations) belong to the same class.
•A class is an abstraction that describes the properties important for an
application
•The choice of classes is arbitrary and application-dependent.
3 2
11
ABSTRACTION
Car
Car
-model: string
-model: string
-Year: string
-year: int
-problem: string
-licenseNumber: string
-owner: string
-motorCapacity: int
-balance: float
-isFinished: bool
+Car (int, ….): void
+Car (string,...): void
+getMake (): string
+printBlanace (): string
+printDetails(): void
+printDetails(): void
+........
+........
In Department of At the
Motor Vehicles mechanic
12
ENCAPSULATION
13
ENCAPSULATION
14
ENCAPSULATION
void sort () { // Bubble Sort
int i, j;
for (i = length - 1; i > 0; i-) {
List for (j = 0; j < i; j++) {
- items: int [ ] if (items [j] > items [j + 1]) {
- length: int int temp = items [j];
items [j] = items [j + 1];
+ List (array):void items [j + 1] = temp;
+ search (int): bool }
+ getMax (): int }
+ sort(): void }
}`
16
INHERITANCE
17
HOW TO USE INHERITANCE?
Taxi Truck
18
POLYMORPHISM
•Polymorphism means that the same operation may behave differently for
different classes.
•An operation is a procedure or a transformation that the object performs or
is subject to.
•An implementation of an operation by a specific class is called a method.
•Because an OO operation is polymorphic, it may have more than one
method for implementing it, each for a different class.
19
POLYMORPHISM
Shape Italic means
- color: int operation is
specified but not
+ Shape (int): void implemented in
+ getColor ():int the base class
+ setColor (int): void
+ getArea(): float
•Generic Class ; the data types of one or more of its attributes are supplied at
run-time (at the time that an object of the class is instantiated).
•This means that the class is parameterized, i.e., the class gets a parameter
which is the name of another type.
• T will be known
• at runtime
Sorter
- data [ ]: T
+ sortData ():void
+ ……. 21
CLASS MODEL
26
OBJECTS
27
CLASS
28
CLASS MODEL
29
VALUES AND ATTRIBUTES
31
OPERATIONS AND METHODS
•
Financial Asset
An operation is a function or procedure that may be
applied to or by objects of a class. -type: int
32
SUMMARY OF CLASS NOTATION
33
SUMMARY OF BASIC CLASS NOTATION
17
34
A SAMPLE CLASS MODEL
Model the classes in a system that represents flights. Each city has at least an
airport. Airlines operate flights from and to various airports. A flight has a list
of passengers, each with a designated seat. Also a flight uses one of the
planes owned by the operating airline. Finally a flight is run by a pilot and a
co-pilot.
35
A SAMPLE CLASS MODEL
• Model the classes in a system that represents flights. Each city has at least
an airport. Airlines operate flights from and to various airports. A flight has
a list of passengers, each with a designated seat. Also a flight uses one of
the planes owned by the operating airline. Finally a flight is run by a pilot
and a co-pilot.
36
A SAMPLE CLASS MODEL
• Flights • City • Airlines
• List of Passengers • Seat • Planes
• Pilot and a Co-Pilot
Passenger
37
CONTINUING CLASS DIAGRAMS
Associations
Multiplicity
Links and Associations
Roles
Self-Associations
Aggregation & Composition Relation
Generalization, Specialization & Inheritance
Overriding Features
Abstract Class
22
ASSOCIATIONS
39
ASSOCIATIONS
INTRODUCTIONTOMODELLINGUSINGUML(CLASSMODELS)
40
ASSOCIATIONS
OwnsStock
Person Company
* *
WorksFor
* 0..1
41
ASSOCIATIONS
42
MULTIPLICITY
• Multiplicity specifies the number of instances of one class that may relate to
a single instance of an associated class.
43
MULTIPLICITY
For example, if a person WorksFor a company, can he work for more than one
company? In other words, is it one-to-one or one-to-many association?
Multiplicity exposes hidden assumptions in the model
44
MULTIPLICITY VALUES
45
LINKS AND
ASSOCIATIONS
A link is a physical or conceptual connection
among objects
• For example John works for GE company.
47
LINKS AND ASSOCIATIONS
48
ROLES
49
SELF ASSOCIATIONS
Pre-req.
0..2
Course
50
ASSOCIATION CLASSES
15
ASSOCIATION CLASSES
52
EXERCISES
Sketch a class diagram for the following:
Each person works for a company receives a salary and has a job title. The
person can work for more than one company. The boss evaluates the
performance of each worker.
53
EXERCISES
Sketch a class diagram for the following:
A user may be authorized on many workstations. Each authorization has a
priority and access privileges. A user has a home directory for each authorized
workstation, but several workstations and users can share the same home
directory.
54
ASSOCIATION CLASSES VS. ORDINARY
CLASSES
Association class OwnsStock has only one occurrence for each pair of person
and company (i.e. only one link exists between pair of person and company
objects ).
In contrast there can be any number of purchase objects for each pair of
person and company objects
QUALIFIED ASSOCIATION
A qualified association is an association in which an attribute called the
qualifier is used to reduce a many relation to one relation on the other end.
Not qualified
1 * File
Directory
filename
qualified
1 0..1
Directory File
filename
56
QUALIFIED ASSOCIATION
57
A SAMPLE CLASS MODEL
• Model the classes in a system that represents flights. Each city has at least an
airport. Airlines operate flights from and to various airports. A flight has a list
of passengers, each with a designated seat. Also a flight usesone of the
planes owned by the operating airline. Finally a flight is run by a pilot and a
co-pilot.
58
The order of modeling is:
• Define classes
• Define associations
• Define multiplicity
59
A SAMPLE CLASS MODEL
• Model the classes in a system that represents flights. Each city has at least
an airport. Airlines operate flights from and to various airports. A flight has
a list of passengers, each with a designated seat. Also a flight uses one of
the planes owned by the operating airline. Finally a flight is run by a pilot
and a co-pilot.
60
A SAMPLE CLASS MODEL
• Flights • City • Airlines
• List of Passengers • Seat • Planes
• Pilot and a Co-Pilot
Passenger
61
A SAMPLE CLASS MODEL
• Model the classes in a system that represents flights. Each city has at least
an airport. Airlines operate flights from and to various airports. A flight has
a list of passengers, each with a designated seat. Also a flight uses one of
the planes owned by the operating airline. Finally a flight is run by a pilot
and a co-pilot.
62
A SAMPLE CLASS MODEL
• Add some suitable attributes and operations to the classes of the flight
system.
28
FURTHER CLASS CONCEPTS
AGGREGATION RELATION
29
AGGREGATION RELATION
66
FURTHER CLASS CONCEPTS
COMPOSITION RELATION
30
FURTHER CLASS CONCEPTS
AGGREGATION & COMPOSITION RELATION
68
FURTHER CLASS CONCEPTS
GENERALIZATION, SPECIALIZATION AND
INHERITANCE
•Generalization is the relationship between class (the superclass) and
one or more variations of the class (the subclass).
•The superclass holds common attributes, operations and associations.
•The subclasses add specific attributes, operations and
associations.
•Each subclass is said to inherit the features of the its superclass.
• Inheritance is called “is-a” relationship.
6
9
GENERALIZATION, SPECIALIZATION AND
INHERITANCE
• The terms generalization, specialization and inheritance refer to
aspects of the same idea, they are used in place of one another .
• Generalization refers to the super-class generalizing its subclasses
• Specialization refers to the fact that the subclass specializes or
refines the super-class,
• Inheritance refers to the mechanism for implementing generalization
/ specialization.
70
GENERALIZATION, SPECIALIZATION AND
INHERITANCE
71
35
USE OF GENERALIZATION
73
OVERRIDING FEATURES
• A subclass may override a superclass feature by defining a feature with the same
name.
• The overriding feature (in the sub-class) replaces the overridden feature (in the
super-class)
• Never override a feature so that it is inconsistent with the original inherited feature.
• Overriding should preserve the attribute type, number and type of arguments
of an operation and its return type.
7
4
GENERALIZATION, SPECIALIZATION AND
INHERITANCE
Shape
- color: int
39
A generalization hierarchy
77
78
Class diagram Example
79