Title: OBJECTORIENTED MODELING WITH UML
1COMP 211INTRODUCTION TOSOFTWARE ENGINEERING
- OBJECT-ORIENTED MODELING WITH UML
2OBJECT-ORIENTED MODELING WITH UML OUTLINE
2
- Overview of UML
- Modeling and Abstraction
- OO modeling
- aggregation, classification and generalization
- Objects and Classes
- state behaviour
- attributes operations
- Generalization
- inheritance
- coverage
- Links and Associations
- multiplicity
- aggregation composition
3WHAT IS UML?
2.2
- a general purpose visual modeling language for
systems - incorporates current best practice in modeling
techniques and software engineering - software development methodology neutral
- industry standard OO modeling language (but can
also be used for non-OO systems) - basic premise software systems can be modeled
as collections of collaborating objects
4UML STRUCTURE
- Building blocks
- things
- relationships
- diagrams
- Common mechanisms
- specifications
- adornments
- common divisions
- extensibility mechanisms
- Architecture
- use-case view
- logical view
- process view
- implementation view
- deployment view
5UML STRUCTURE (contd)
- Building blocks - basic UML modeling elements
- things the modeling elements classes,
interfaces, use case, interactions, packages,
notes (can be structural, behavioural, grouping
or annotational) - relationships specify how two or more things are
semantically related - diagrams views into UML models show collections
of things that visualize what the system will do
or how it will do it - Common mechanisms - common ways of achieving
specific goals - specifications textual descriptions of the
semantics of a modeling element - adornments additional, visual details about
modeling elements - common divisions particular ways of thinking
about the world (e.g., classifier-instance
interface-implementation) - extensibility mechanisms ways to customize the
modeling elements for specific needs
6UML STRUCTURE (contd)
- Architecture - the strategic aspects of the
high-level structure of the system that is
captured in UML by - use-case view captures the basic requirements
for the system and provides the basis for the
construction of the other views - logical view captures the vocabulary of the
problem domain as a set of classes and objects - process view models the executable threads and
processes in the system - implementation view models the files and
components that make up the physical code base of
the system - deployment view models the physical deployment
of components onto computational nodes such as
computers and peripherals
7OBJECT-ORIENTED MODELING
2.3
- to abstract reality
- show essential details filter out non-essential
details - to help us deal with complexity
- deal with human limitations in understanding
complex things - to allow us to focus on the big picture
- to promote understanding of requirements, cleaner
design, more maintainable systems - Why objects?
- to more accurately reflect reality in a model
- to reduce the semantic gap between reality and
a model - to localize changes to the model
- We can model reality as a number of interacting
objects
8OBJECT-ORIENTED MODELING PERSPECTIVES
- conceptual
- do not consider any aspects of implementation of
objects - Focus on identifying the objects in the problem
domain
- specification
- consider interfaces of objects (but no internal
aspects) - Focus on how objects interact in the solution
domain - implementation
- consider all details of objects (external and
internal) - Focus on how to code objects
- Modeling proceeds from the conceptual to
implementation - Same OO concepts can be used at all levels to
build models
9ABSTRACTION
2.3.2
- mental process of selecting some object
characteristics and properties to model and
excluding others that are not relevant
Q
- wings,
landing gear, ...
flies,
- abstraction is always for some purpose
- many different abstractions of the same thing are
possible - all abstractions are incomplete descriptions of
reality - We do not need completeness, just modeling
adequacy!
10QUESTION?
If you had to draw a map of the area around HKUST
to explain to someone how to get to HKUST from
Choi Hung, what is the one most important thing
you would include?
- roads.
- land topology (e.g., contours showing elevation
above sea level. - historical sites to see (e.g., the HKUST beach).
- the types and places of vegetation (i.e., trees
and other types of plants). - location of hiking trails.
- bus routes.
11QUESTION?
If you had to draw a map of the area around HKUST
to explain to someone how high the Academic
Building is above sea level, what is the one most
important thing you would include?
- roads.
- land topology (e.g., contours showing elevation
above sea level. - historical sites to see (e.g., the HKUST beach).
- the types and places of vegetation (i.e., trees
and other types of plants). - location of hiking trails.
- bus routes.
12TYPES OF ABSTRACTIONS
2.3.2
- classification group similar instances of
objects
DBMS software
Microsoft software
IS_MEMBER_OF relationship
IS_MEMBER_OF relationship
Sybase
. . .
Access
Oracle
Word
PowerPoint
. . .
- pick out common properties and ignore unique
properties
13TYPES OF ABSTRACTIONS (contd)
- aggregation group dissimilar sets of objects
airplane
IS_PART_OF relationship
fuselage
. . .
wing
landing gear
- ignore differences among the parts and
concentrateon the fact that they form the whole
14TYPES OF ABSTRACTIONS (contd)
- generalization group similar sets of objects
superclass/supertype
student
IS_A relationship
postgrad
. . .
secondary
undergrad
subclass/subtype
- note difference between classification and
generalization - classification applied to individual instances
of objects - generalization applied to sets of objects
(classes)
15TYPES OF ABSTRACTIONS (contd)
- Note we can combine different types of
abstraction when modeling
student
generalization
undergrad
classification
John
. . .
Sam
Tina
aggregation
. . .
name
student id
16QUESTION?
- Consider the relationship between files and
directories in a computer system. Which type(s)
of abstraction models this relationship?
- classification only
- aggregation only
- generalization only
- classification and aggregation
- aggregation and generalization
- classification and generalization
- classification, aggregation and generalization
- none of the above
17OBJECT
2.3.4 2.4.2
a concept, abstraction or thing with crisp
boundaries and meaning which plays a role with
respect to a request for an operation
- interface describes the operationswhich an
object can perform
18OBJECT PROPERTIES
Freds Savings Account
identity
19OBJECT PROPERTIES
- identity unique existence in time and space
- often referenced by a unique identifier, but this
is not the same as the object - represented by an object id (OID) for
implementation purposes - state static properties of objects that are
persistent (attributes) - fixed number of attributes
- either mutable (can be changed) or immutable
(cannot be changed) - behaviour dynamic properties of objects that
describe how objects communicate with each
other (operations) - fixed number of operations
- operations invoked by messages received from
other objects - message handling may be state dependent
- operations may change an objects state
(attribute values) - in many models
- only state or behaviour is emphasized, or
- they are treated separately
- in object-oriented models they are considered
together since objects contain both aspects
20OBJECT ENCAPSULATION
a kind of abstraction that enforces a (clean)
separation between the external interface and the
internal implementation of an object
user
request
request
response
response
- hides unnecessary details of implementation
(information hiding) and ensures modularity - an object has a visible (public) interface and a
hidden (private) implementation - public interface provides services to other
objects
21(OBJECT) CLASS
2.3.4 2.4.2
- a description of a group of objects that have
common - state relationships
- behaviour semantics
- provides a template to create objects (a
factory for objects) - a class is a classifier an object is an instance
- should capture only one abstraction
- one major theme
- should be named using the vocabulary of the
problem domain - Most languages allow a class to behave as
thoughit were an object in its own right
22WHY CLASSES?
2.3.4 2.4.2
- conveniently describes a collection of objects
abstraction - reduces complexity makes systems easier to
understand - write-once common definition defined and stored
once - specifies what values are acceptable in a given
context - allows compilers (and people) to understand the
programmers intention and to do certain types of
error checking - Choosing appropriate classes is animportant
design decision - How are classes and modules related?
- classes can be thought of asloosely coupled,
highly cohesive modules
23QUESTION?
- Which type of abstraction relates objects and
classes? - classification only
- aggregation only
- generalization only
- classification and aggregation
- aggregation and generalization
- classification and generalization
- classification, aggregation and generalization
- none of the above (this is not a type of
abstraction)
24CLASS ATTRIBUTE
2.3.4
a description of data values held by objects in a
class
- each attribute has a
- name (unique within a class, but not across
classes)
- type (specifies the domain of values string,
integer, money, etc. from which an attribute
can take its values) - continuous (e.g., salary) ? discrete (e.g., sex)
- visibility public (), private (-), protected
(), package () - initial value (optional)
- multiplicity (optional number of simultaneous
values,e.g., telephone number default is
exactly 1) - changeability frozen, addOnly, no constraint
(default) - base attribute versus derived attribute
- e.g., birthdate versus age
25ATTRIBUTES UML NOTATION
- stereotype visibility name multiplicity
typeExpression initialValue propertyString - propertyString gt a comma separated list of
properties or constraints - only name is mandatory
- Examples
- size area (100,000) frozen
- name string
- telephone0..2 string
- telephone1, 3..4 string
- - salary money gt0, lt1,000,000
- You are not required to know this syntax!
26QUESTION?
- Which type of abstraction relates attributes and
classes? - classification only
- aggregation only
- generalization only
- classification and aggregation
- aggregation and generalization
- classification and generalization
- classification, aggregation and generalization
- none of the above (this is not a type of
abstraction)
27CLASS OPERATION
2.3.4 2.4.2
a function or transformation that may be applied
to or by objects in a class
- Company hire, fire, pay-dividends,
- Course register, waive-prerequisite,
- invoked by a message sent to an object
- operation signature name of operation (called
selector), number and types of the arguments, and
type of the result value - visibility public (), private (-), protected
(), package () - side effects if the execution of an operation
changes the state of an object (query operation
no side effects)
28OPERATION UML NOTATION
- stereotype visibility name (parameterList)
returnType propertyString - parameterList gt kind name typeExpression
defaultValue - kind--gt in - pass by value
- out - pass by reference (no input value output
value only) - inout - pass by reference (input and output
value) - propertyString gt a comma separated list of
properties or constraints - isQuery true or false
- isPolymorphic true or false
- concurrency sequential - callers must
coordinate to ensure only one call to an
object may execute at one time - guarded - multiple calls to an object may
occur simultaneously, but only one is
allowed to execute at a time other calls are
blocked - concurrent - multiple calls may occur
simultaneously to an object and all execute
concurrently - operation signature name (parameterList)
returnType - You are not required to know this syntax!
29CLASS METHOD
2.4.2
the implementation of an operation for a class
- polymorphic operation an operation that can
have several different methods - class operation
- Account payInterest
- Savings calculate interest on savings accounts
- Checking calculate interest on checking accounts
- dynamic binding choosing the method to execute
for an operation based on the objects class
How does a requesting object know whether the
returned value is the result of accessing an
attribute value or of a calculation performed by
a method?
30SCOPE
- attributes and operations normally have instance
scope(i.e., they apply to/operate on a single
object instance) - sometimes, it is useful to have attributes and
operations that have class scope (i.e., they
apply to/operate on an entire class of objects) - attributes whose values are class determined a
counter - object constructors create new object instances
- in UML notation, such attributes/operations are
underlined
31CLASS UML GRAPHICAL NOTATION
Visibility public - private protected
package
32GENERALIZATION
2.4.2
- a relationship between a class and one or more
refined versions
generalization
Vehicle
vehicle-type
Van
Car
Truck
discriminator
car-size
Compact
Midsize
Fullsize
specialization
discriminator an attribute of enumeration type
that indicates which property of a class is
being abstracted by a generalization
33GENERALIZATION INHERITANCE
the assumption of properties by a subclass from
its superclasses
- extract similarities (common attributes and
operations), put them in a superclass and inherit
them to subclass(es) - reduces redundancy of descriptions
- promotes reusability of descriptions
- only define properties and behaviour in one
place - allows substitution of an object of a
superclasswith an object of the subclass
34GENERALIZATION INHERITANCE (contd)
- conflict resolution by
- user-defined order
- predefined order
- redefine names(attributes only)
- subclass may
- add new properties (attributes, operations)
Single inheritance
Multiple inheritance
Which withdraw method should CheckingSavings
inherit?
35MULTIPLE INHERITANCE EXAMPLE
id
id
id
id
id
id
? id
id ?
36MULTIPLE INHERITANCE EXAMPLE
id
id
id
id
? id
id ?
37MULTIPLE INHERITANCE EXAMPLE
weight
displacement
name conflict
38GENERALIZATION ABSTRACT CLASS
a class that has no direct instances
- used for modeling purposes as a container for
definitions, but no instances of the class are of
interest
Note Operations may also be abstract ? no
method specified Shown in UML by using italics
39GENERALIZATION COVERAGE
overlapping - a superclass object can be a member
of more than one subclass
disjoint - a superclass object is a member of at
most one subclass
40GENERALIZATION COVERAGE (contd)
complete - all superclass objects are also
members of some subclass
incomplete - some superclass object is not a
member of any subclass
41GENERALIZATION COVERAGE (contd)
overlapping, incomplete
overlapping, complete
42GENERALIZATION COVERAGE (contd)
disjoint, complete
disjoint, incomplete
43QUESTION?
What is the policy of the organization?
44QUESTION?
What is the policy of the organization?
45LINK
2.4.2
a physical or conceptual relationship between
object instances
Teaches
Works-for
- links allow one object to send messages to
another object
46ASSOCIATION
2.4.2
- a description of a group of links with common
semantics
Teaches
u Teaches
Person
Course
- can show navigability ofassociations(implies
that the source object has a reference to the
target object)
class diagram
Works-for
- can show readability ofassociations
- an association is a classifier a link is an
instance
- conceptually, associations are inherently
bi-directional
47ASSOCIATION (contd)
- there can be several associations between the
same two classes
Attended
Person
University
Works-for
Attended
Works-for
- or even with the same class
Person
Manages
Married-to
48QUESTION?
Which type of abstraction relates links and
associations?
- classification only
- aggregation only
- generalization only
- classification and aggregation
- aggregation and generalization
- classification and generalization
- classification, aggregation and generalization
- none of the above (this is not a type of
abstraction)
49ASSOCIATION DEGREE
- unary (reflexive)
- relates a class to itself
- n-ary - relates any number of classes
- In practice, the vast majority of associations
are binary
50ASSOCIATION DEGREE (contd)
- can this be expressed as two binary
associations? - suppose we know John has account 532 with HSBC
- John has account 121 with BoC
- Mary has account 993 with BoC
X
?
X
?
v
?
51ASSOCIATION DEGREE (contd)
532
121
993
- Cannot tell which of Johns accounts is with
which bank
52ASSOCIATION DEGREE (contd)
John
121
532
Mary
993
- Cannot tell which BoC account belongs to which
customer
53ASSOCIATION DEGREE (contd)
532
121
993
John
Mary
54ASSOCIATION DEGREE (contd)
- can this be expressed as two binary
associations? - suppose we know Mary works on the Accounting
project using Cobol - Mary works on the CAD project using C
- John works on the Accounting project using C
X
?
X
?
X
?
55ASSOCIATION DEGREE (contd)
- Cannot tell that John uses only C on Accounting
56ASSOCIATION DEGREE (contd)
- Cannot tell which language Mary uses on which
project
57ASSOCIATION DEGREE (contd)
- Cannot tell that John works only on Accounting
58ASSOCIATION DEGREE (contd)
59ASSOCIATION MULTIPLICITY
2.4.2 5.3.2
- specifies restrictions on the number of objects
in each class that may be related to objects in
another class
0..
1..
Instructor
Course
For a given course, how many instructors can
teach it?
- each course must be taught by one instructor but
may have many
For a given instructor, how many courses can he
teach?
- an instructor does not have to teach any course
but may teach more than one course in a given
semester
60ASSOCIATION MULTIPLICITY (contd)
A
c..d
a..b
C1
C2
max-card(C1,A)
min-card(C1,A)
- minimal cardinality (min-card)
- min-card(C1,A) minimum number of links in which
each object of C1 can participate in A
min-card(C1,A) 0 ? optional participation min-ca
rd(C1,A) gt 0 ? mandatory participation
- maximal cardinality (max-card)
- max-card(C1,A) maximum number of links in which
each object of C1 can participate in A
61ASSOCIATION MULTIPLICITY (contd)
- max-card ? an unlimited upper bound (?)
- min-card 1 and max-card 1 ? can use 1 by
itself - min-card 0 and max-card ? can use by
itself - max-card(C1,A) 1 and max-card(C2,A) 1
- one-to-one association (11)
- max-card(C1,A) 1 and max-card(C2,A)
- one-to-many association (1N)
- max-card(C1,A) and max-card(C2,A)
- many to many association (NM)
62ASSOCIATION MULTIPLICITY (contd)
- one-to-one association (11)
0..1
1..1
1
Capital-of
63ASSOCIATION MULTIPLICITY (contd)
one-to-many association (1N)
1
1..1
0..
Assigned-to
64ASSOCIATION MULTIPLICITY (contd)
many to many association (NM)
Supplies
65QUESTION?
?
?
10..45
1..5
- A student must enroll in at least one course and
can enroll in at most five courses - A course must have at least ten students enrolled
in it and cannot have more than forty-five
students enrolled in it.
66N-ARY ASSOCIATION MULTIPLICITY
- defined with respect to the other n-1 ends
- given a ternary association among classes (A, B,
C), the multiplicity of the C end states how many
C objects may appear in association with a
particular pair of (A, B) objects
0..1
? 0 or 1 professor
- for each (course, student)
- a student will not take the same course from more
than one professor, but a student may take more
than one course from one professor and a
professor may teach more than one course
- for each (student, professor)
? many courses
- for each (course, professor)
? many students
67QUESTION?
0..3
0..1
? 0, 1, 2 or 3 days
? 0 or 1 classrooms
? many courses
68QUESTION?
What is the most likely multiplicity of the
following associations?
69EXAMPLE
- The classes shown have attributes that are really
pointers (OIDs) to other classes and which should
be replaced with relationships. A person may work
for any number of companies. Each person has an
ID. A car is assigned an ID. Persons, companies,
or banks may own cars. Car owner ID is the ID of
the person, company, or bank who owns the car. A
car may have only one owner (person, company, or
bank). A car loan given by a bank may be
involved in the purchase of a car. - Prepare a class diagram in which the pointers are
replaced with relationships. Use associations and
generalizations as necessary. Show the most
likely multiplicities for all associations and
the final attributes for each class. - Note Your final class diagram should contain no
IDs.
70EXAMPLE
71AGGREGATION/COMPOSITION ASSOCIATION
2.4.2
- a special type of association in which there is a
part-of relationship between one class and
another class
Has
Disk
Computer
- a component may exist independent of the
aggregate object of which it is a part ?
aggregation
Has
Course-offering
Course
- a component may not exist independent of the
aggregate object of which it is a part ?
composition
72AGGREGATION/COMPOSITION (contd)
- sometimes it is not clear whether the parts
should be related with or
Car
- Which aggregation association to use is a matter
of how one interprets reality and/or the
requirements of the application
73WHEN TO USE AGGREGATION/COMPOSITION?
- Would you use the phrase part of to describe
the association or name it Has? (But be
CAREFUL! Not all Has associations are
aggregations.) - Are operations on the whole automatically applied
to the part(s) composition? - Are some attribute values propagated from the
whole to all or some of the parts? - Is there an intrinsic asymmetry to the
association where one object class is subordinate
to the other(s)?
- It is not wrong to use association rather than
aggregation! (When in doubt, use association!)
74ASSOCIATION ROLE NAMES
2.4.2
- role one end of an association
employee
employer
- for binary associations there are two roles
75ASSOCIATION ROLE NAMES (contd)
role one end of an association
Boss
Person
Manages
Worker
Boss
Worker
Boss
- It is necessary to use role names when an
associationrelates objects from the same class
76ASSOCIATION ASSOCIATION CLASS
2.4.2
What about an attribute like grade?
A
B
A-
C
A
B
77ASSOCIATION ASSOCIATION CLASS (contd)
- Possibility 1 use many attributes for grade
(a) in Student
(b) in Course
78ASSOCIATION ASSOCIATION CLASS (contd)
- Possibility 2 use a multi-valued attribute for
grade
(a) in Student
(b) in Course
79ASSOCIATION ASSOCIATION CLASS (contd)
- Possibility 3 use an association class
80ASSOCIATION ASSOCIATION CLASS (contd)
- When to associate an attribute with an
association?
?
salary
- Usually needed only for many to many
relationships!
81ASSOCIATION CONSTRAINTS
subset
general
82UML RELATIONSHIPS
- association describes links among object
instances (only relationship that relates object
instances) - generalization relates a more general class
(superclass) to a more specific kind of the
general class (subclass) - dependency relates classes whose behaviour or
implementation affect other classes - flow relates two versions of an object at
successive times - usage shows that one class requires another class
for its correct functioning - realization relates a specification to its
implementation (e.g., an interface to the
classes that implement it)
83STEREOTYPE
a new class of modeling element, which is a
subclass of an existing modeling element
- allows the object model to be dynamically
extended - Example we can define different kinds of classes
that are useful for modeling
boundary OrderForm
entity BankAccount
control PenTracker
- special icons can be used for each stereotype
84CLASS DIAGRAM EXAMPLE
85CLASS DIAGRAM EXAMPLE
86EXAMPLE
- Construct a class diagram for the classes listed
below. Use association, aggregation and
generalization relationships as necessary. Show
multiplicity for all relationships and role names
where necessary. - file system drive file directory
- sector ASCII file disk executable file
- track
87SUMMARY OBJECT-ORIENTED MODELING
classifier instance class object attribute
value operation method association link