You are on page 1of 74

System System Analysis Analysis & & Design Design

OBJECT-ORIENTED OBJECT-ORIENTED MODELING MODELING

COMP 211

OO MODELING WITH UML1

OBJECT-ORIENTED MODELING OUTLINE

Modeling and Abstraction


OO modeling aggregation, classification and generalization

Objects and Classes


state & behavior attributes & operations

Generalization
inheritance

Links and Associations


multiplicity aggregation & composition

COMP 211

OO MODELING WITH UML2

OBJECT-ORIENTED MODELING

[2.3]

Why models?
to abstract reality
filter out non-essential details

to help us deal with complexity


deal with human limitations

to allow us to focus on the big picture to promote understanding, cleaner design, maintainable systems

Why objects?
to reflect reality more accurately to reduce the semantic gap between reality and a model

We model reality as a number of interacting objects


COMP 211 OO MODELING WITH UML3

OBJECT-ORIENTED MODELING PERSPECTIVES


conceptual
do not consider any aspects of implementation of objects

focus on identifying the objects in the application 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)


COMP 211

focus on how to code objects

same OO concepts can be used at all levels to build models


OO MODELING WITH UML4

ABSTRACTION

[2.3.2]

mental process of selecting some characteristics and properties and excluding others that are not relevant

- wings, flies, landing gear, ...

abstraction is always for some purpose many different abstractions of the same thing are possible all abstractions are incomplete descriptions of reality

COMP 211

We do not need completeness, just modeling adequacy!


OO MODELING WITH UML5

QUESTION?
If you had to draw a map of the area around HKUST to explain to someone someone how how to get to get to HKUST to HKUST from from Choi Choi Hung, Hung, what what is the is the one most one important most important thing you thing would you include? would include?
1) roads. 2) land topology (e.g., contours showing elevation above sea level. 3) historical sites to see (e.g., the HKUST beach). 4) the types and places of vegetation (i.e., trees and other types of plants). 5) location of hiking trails. 6) bus routes.

COMP 211

OO MODELING WITH UML6

QUESTION?
If you had to draw a map of the area around HKUST to explain to someone someone how how high high the the Academic Academic Building Building is above is above seasea level, level, what is the what one ismost the one important most important thing youthing would you include? would include?
1) roads. 2) land topology (e.g., contours showing elevation above sea level. 3) historical sites to see (e.g., the HKUST beach). 4) the types and places of vegetation (i.e., trees and other types of plants). 5) location of hiking trails. 6) bus routes.

COMP 211

OO MODELING WITH UML7

TYPES OF ABSTRACTIONS

[2.3.2]

classification group similar instances of objects


Microsoft software DBMS software IS_MEMBER_OF relationship

IS_MEMBER_OF relationship

Word

PowerPoint

. . .

Access

Oracle

. . .

Sybase

pick out common properties and ignore unique properties

COMP 211

OO MODELING WITH UML8

TYPES OF ABSTRACTIONS (contd)

aggregation group dissimilar sets of objects

airplane
IS_PART_OF relationship

wing

landing gear

. . .

fuselage

ignore differences among the parts and concentrate on the fact that they form the whole

COMP 211

OO MODELING WITH UML9

TYPES OF ABSTRACTIONS (contd)

generalization group similar sets of objects


superclass/supertype

student
IS_A relationship

secondary

undergrad

. . .

postgrad

subclass/subtype

note difference between classification and generalization

classification applied to individual instances of objects generalization applied to sets of objects (classes)
COMP 211 OO MODELING WITH UML10

TYPES OF ABSTRACTIONS (contd)


student
generalization

undergrad
classification

Sam

Tina

. . .

John
aggregation

name

student id

. . .

COMP 211

OO MODELING WITH UML11

QUESTION?
Consider the relationship between files and directories in a computer system. Which type(s) of abstraction models this relationship?

1) classification only 2) aggregation only 3) generalization only 4) classification and aggregation 5) aggregation and generalization 6) classification and generalization 7) classification, aggregation and generalization directory 8) none of the above

file

COMP 211

OO MODELING WITH UML12

OBJECT

[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

Freds Savings Account


Balance Deposit Withdraw

COMP 211

interface describes the operations which an object can perform


OO MODELING WITH UML13

OBJECT CONTENTS Freds Savings Account


Account number (integer value)

state
Amount (money value) Balance Method (code)

Balance Balance
Deposit Method (code)

Deposit Deposit
Withdraw Method (code)

behavior

Withdraw Withdraw
Pay-interest Method (code)

Pay interest
COMP 211 OO MODELING WITH UML14

OBJECT ENCAPSULATION
a kind of abstraction that enforces a (clean) separation between the external interface and the internal implementation of an object

object 1 user
request response

object 2 interface
request response

interface

hides unnecessary details of implementation (information hiding) and ensures modularity an object has a visible (public) interface and a hidden (private) implementation
OO MODELING WITH UML16

COMP 211

OBJECT PROPERTIES

each object has a unique, immutable identity independent of its current state or behavior OID

objects communicate by responding to stimuli that invoke their operations message passing

Freds Savings Account

Deposit(...)
Teller Alice

objects choose how/whether they will respond to a message/stimulus object independence

COMP 211

OO MODELING WITH UML17

(OBJECT) CLASS
state behavior

[2.3.4; 2.4.2]

a description of a group of objects that have common:


- relationships - semantics

objects in a class share a common semantic purpose

Why classes? convenient way of describing a collection of objects specify what values are acceptable in a given context How are classes and modules related? classes can be thought of as loosely coupled, highly cohesive modules
COMP 211 OO MODELING WITH UML18

QUESTION?
Which apply type classification of abstraction abstraction relates objects to objects and to classes? form classes

1) classification only 2) aggregation only 3) generalization only 4) classification and aggregation 5) aggregation and generalization 6) classification and generalization
IS_MEMBER_OF relationship

7) classification, aggregation and generalization 8) none of the above (this is not a type of abstraction)

COMP 211

OO MODELING WITH UML19

CLASS ATTRIBUTE
a description of data values held by objects in a class

each attribute has:


name (unique within a class) type (specifies the domain of values from which an attribute can take its values)
continuous discrete

visibility: public (+), protected (#), private (-) initial value (optional) multiplicity (optionaldefault is exactly 1) changeability: frozen, addOnly, no constraint (default)

base attribute versus derived attribute


age versus birthdate

COMP 211

OO MODELING WITH UML20

ATTRIBUTES UML NOTATION

<<sterotype>> visibility name [multiplicity]: type-expression = initialvalue {property-string} property-string -> a comma separated list of properties or constraints

Examples
+ size: Area = (100,000) {frozen} name: string telephone[0..2]: string telephone[1, 3..4]: string - salary: Money {>0, <1,000,000}

COMP 211

You are not required to know this syntax!


OO MODELING WITH UML21

Question?
apply aggregation abstraction Which type of abstraction relates

to attributes to classes? form classes attributes and

1) classification only 2) aggregation only 3) generalization only Course 4) classification and aggregation 5) aggregation and generalizationIS_PART_OF 6) classification and generalization relationship 7) classification, aggregation and generalization course# 8) none of the above (this is not a type of abstraction) name instructor prerequisite

COMP 211

OO MODELING WITH UML22

CLASS OPERATION
a function or transformation that may be applied to or by objects in a class

invoked by a message sent to an object operation signature: name of operation (selector), number and types of the arguments, and the type of the result value visibility: public (+), protected (#), private (-) side effects: if the execution of an operation changes the state of an object (query operation no side effects)

COMP 211

OO MODELING WITH UML23

OPERATION UML NOTATION


<<sterotype>> visibility name (parameter-list): return-type {property-string}
parameter-list -> kind name: type-expression = default-value
kind-> in - pass by value out - pass by reference (no input value ; output value only) inout - pass by reference (input and output value)

property-string -> a comma separated list of properties or constraints


isQuery = true or false isPolymorphic = true or false concurrency = sequential call to guarded execute an callers must coordinate to ensure only one an object may execute at one time

- multiple calls to an object may occur simultaneously, but only one is allowed to at a time; other calls are blocked multiple calls may occur simultaneously to object and all execute concurrently

concurrent -

COMP 211

You are not required to know this syntax!


OO MODELING WITH UML24

CLASS METHOD
the implementation of an operation for a class

polymorphic operation an operation that can have several different methods


class File
ASCII file digitized file

operation print
ASCII file print method digitized file print method

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?

COMP 211

OO MODELING WITH UML25

CLASS UML GRAPHICAL NOTATION Class name attribute compartment operation compartment extra compartment
derived attribute Visibility
+ public - private # protected

Company +name: string +address: string +stock price: money +profit: money /pe ratio: decimal #hire #fire -pay-dividend

COMP 211

OO MODELING WITH UML26

GENERALIZATION

[2.4.2]

a relationship between a class and one or more refined versions generalization


superclass

Vehicle
vehicle-type

subclass

Van
superclass

Car
car-size

Truck
discriminator

Compact
subclass

Midsize

Full-size specialization

discriminator: an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization
COMP 211 OO MODELING WITH UML27

GENERALIZATION 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 promotes reusability

only define properties and behaviour in one place


an object of a subclass should be substitutable for an object of the superclass
OO MODELING WITH UML28

COMP 211

GENERALIZATION INHERITANCE (contd)


subclass may
add new properties (attributes, operations) override methods

Account
account-number amount balance deposit withdraw

conflict resolution by:


user-defined order predefined order redefine names (attributes only)

Checking
service-charge withdraw

Savings
interest-rate withdraw

Checking Savings
service-charge interest-rate withdraw
COMP 211 OO MODELING WITH UML29

MULTIPLE INHERITANCE EXAMPLE


University member
id

id

Student

Employee

id

id

id

id

id

Undergrad

Postgrad

Staff

Faculty

? id
COMP 211

TA

id ?
OO MODELING WITH UML30

MULTIPLE INHERITANCE EXAMPLE

Student
id

Employee
id

id

id

id

id

Undergrad

Postgrad

Staff

Faculty

? id

TA

id ?

COMP 211

OO MODELING WITH UML31

MULTIPLE INHERITANCE EXAMPLE

Vehicle

weight

Land vehicle
size

displacement

Water vehicle
size

Car

Amphibious vehicle
name conflict

Boat

COMP 211

OO MODELING WITH UML32

GENERALIZATION ABSTRACT CLASS


a class that has no direct instances

used as a container for definitions, but no instances of the class are of interest Account

Checking

Savings

Note: operations may also be abstract no method specified


COMP 211 OO MODELING WITH UML33

GENERALIZATION CONSTRAINTS
overlapping - a superclass object can be a member of more than one Player subclass Player

Soccer Female

Tennis

{overlapping}

Tennis

Soccer

disjoint - a superclass object is a member of at most one subclass Person Male


{disjoint}

Person

Male

Female
OO MODELING WITH UML34

COMP 211

GENERALIZATION CONSTRAINTS (contd)


complete - all superclass objects are also members of some subclass University University Student Student Undergrad

incomplete - some superclass object is not a member of any subclass Tree Elm Oak Tree Birch
COMP 211

Postgrad

{complete}

Undergrad

Postgrad

{incomplete}

Oak

Elm

Birch
OO MODELING WITH UML35

GENERALIZATION CONSTRAINTS (contd)


overlapping, incomplete Player

Player

Soccer PG

Tennis

{overlapping, incomplete}

Tennis overlapping, complete Course


{overlapping, complete}

Soccer

Course

UG

UG
COMP 211

PG
OO MODELING WITH UML36

GENERALIZATION CONSTRAINTS (contd)


disjoint, complete University Student Undergrad University Student

disjoint, incomplete Tree Elm Oak Tree Birch


COMP 211

Postgrad

{disjoint, complete}

Undergrad

Postgrad

{disjoint, incomplete}

Oak

Elm

Birch

OO MODELING WITH UML37

QUESTION?
Payment
{disjoint, complete}

Cash

Credit-card

Debit-card

The abstraction generalization shown above is:


1) overlapping, complete 2) disjoint, complete 3) overlapping, incomplete 4) disjoint, incomplete

COMP 211

OO MODELING WITH UML38

QUESTION?
Customer
{overlapping, complete}

WWW

In-store

The abstraction generalization shown above is:


1) overlapping, complete 2) disjoint, complete 3) overlapping, incomplete 4) disjoint, incomplete

COMP 211

OO MODELING WITH UML39

LINK

[2.4.2]

a physical or conceptual relationship between object instances


Person Gan Kok Wee Dik Lee Works-for Beat Wthrich Fred Lochovsky Organization HKUST Boeing Pam Drew Course COMP 111 COMP 337 COMP 211 COMP 332
COMP 211 OO MODELING WITH UML40

Teaches

ASSOCIATION

[2.4.2]

a description of a group of links with common semantics


Teaches Teaches

Person
Works-for

Course

class diagram

Person

Works-for can show navigability of

Organization associations

Organization

can show readability of associations

COMP 211

conceptually, associations are inherently bi-directional


OO MODELING WITH UML41

ASSOCIATION (contd)

there can be several associations between the same two classes


Attended

Person
Works-for Attended Works-for

University

Fred

U Toronto

or even with the same class

Person
Married-to Manages

COMP 211

OO MODELING WITH UML42

QUESTION?
Which apply type aggregation classification of abstraction abstraction abstraction relates to links toclasses links and to associations? to form form associations associations

1) classification only 2) aggregation only


Works-for

3) generalization only 4) classification and aggregation Person 5) aggregation and generalization University 6) classification and generalization 7)Person classification, aggregationUniversity and generalization 8) none of the above (this is not a type of abstraction)

COMP 211

OO MODELING WITH UML43

ASSOCIATION DEGREE

unary
relates a class to itself

Person Manages

binary

Person

Teaches

Course

ternary

Project

Language

Person

n-ary - relates any number of classes

COMP 211

In practice, the vast majority of associations are binary


OO MODELING WITH UML44

ASSOCIATION DEGREE (contd)


Bank

Customer

Account

can this be expressed as two binary associations?

Customer

Has

Account

Is-with

Bank

COMP 211

OO MODELING WITH UML45

ASSOCIATION DEGREE (contd)


Person

Project

Language

Mary

John

CAD

C Cobol

Accounting

COMP 211

can this be expressed as two binary associations?


OO MODELING WITH UML46

ASSOCIATION DEGREE (contd)


Project Person Language

Mary

John

CAD

C Cobol

Accounting

COMP 211

cannot tell which language Mary uses on which project


OO MODELING WITH UML47

ASSOCIATION DEGREE (contd)


Person Project Language

Accounting

CAD

Mary
John

Cobol C

COMP 211

cannot tell that Mary uses only Cobol on Accounting and John uses only C on Accounting
OO MODELING WITH UML48

ASSOCIATION DEGREE (contd)


Project Language Person

C Cobol

CAD

Accounting

John Mary

COMP 211

cannot tell that Mary uses only C on CAD and not on Accounting
OO MODELING WITH UML49

ASSOCIATION MULTIPLICITY

specifies restrictions on the number of objects in each class that may be related to objects in another class
1..1

Instructor

Teaches

0..1

Course

For a given course, how many instructors can teach it?

assume each course can be taught by one and only one instructor

For a given instructor, how many courses can he teach?


assume an instructor does not have to teach any course and may teach at most one course in a given semester

a real-world constraint!
COMP 211 OO MODELING WITH UML50

ASSOCIATION MULTIPLICITY (contd)


C1 min-card(C2,A) max-card(C2,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-card(C1,A) > 0 mandatory participation
a..b

c..d

C2 max-card(C1,A) min-card(C1,A)

maximal cardinality (max-card)


max-card(C1,A): maximum number of links in which each object of C1 can participate in A
COMP 211 OO MODELING WITH UML51

ASSOCIATION MULTIPLICITY (contd)


special cardinalities: 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


COMP 211

one-to-one association (1:1)

max-card(C1,A) = 1 and max-card(C2,A) = *


one-to-many association (1:N)

max-card(C1,A) = * and max-card(C2,A) = *


many to many association (N:M)

OO MODELING WITH UML52

ASSOCIATION MULTIPLICITY (contd)


one-to-one association (1:1) Country
0..1

Capital-of

1..1 1

City

Country China Canada USA

Capital-of

City Beijing Ottawa New York Washington

COMP 211

OO MODELING WITH UML53

ASSOCIATION MULTIPLICITY (contd)


one-to-many association (1:N) Employee
* 0..*

Assigned-to

1..1 1

Department

Employee John James Alan Bill Larry

Assigned-to

Department Sales Production Marketing

COMP 211

OO MODELING WITH UML54

ASSOCIATION MULTIPLICITY (contd)


many to many association (N:M) Supplier
*

Supplies

Part

Supplier E-com Vtech Mach SGH Hi-tech

Supplies

Part 85213 28175 33472 90231

Lansing

COMP 211

OO MODELING WITH UML55

QUESTION?
? 10..45
Enrolled-in

Student

? 1..5

Course

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.

COMP 211

OO MODELING WITH UML56

N-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
Registration

Course

0..1

Professor

Student

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 for each (student, professor) many courses one professor and a professor may teach more than one course for each (course, professor) many students
OO MODELING WITH UML57

COMP 211

QUESTION?
Meets

Course

0..3

Day

0..1

Classroom

for each (course, classroom) for each (course, day) for each (classroom, day)

0, 1, 2 or 3 days 0 or 1 classrooms many courses

COMP 211

OO MODELING WITH UML58

AGGREGATION/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

Computer

Disk

a component may exist independent of the aggregate object of which it is a part aggregation

Course

Has

Course-offering

a component may not exist independent of the aggregate object of which it is a part composition
OO MODELING WITH UML59

COMP 211

AGGREGATION/COMPOSITION (contd)

sometimes it is not clear whether the parts should be related with or Chassis Car Engine

Transmission

Doors

Frame

Which aggregation association to use is a matter of how one interprets reality and/or the requirements of the application
OO MODELING WITH UML60

COMP 211

WHEN TO USE AGGREGATION/COMPOSITION?

Would you use the phrase part of to describe the association or name it Has? 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!


COMP 211 OO MODELING WITH UML61

ASSOCIATION ROLE NAMES

[2.4.2]

role: one end of an association

Person

Works-for employee employer

Company

for binary associations there are two roles

COMP 211

OO MODELING WITH UML62

ASSOCIATION ROLE NAMES (contd)


role: one end of an association
Boss

Person
Worker

Manages

John Sarah

Boss

Worker Frank Bill Boss

COMP 211

OO MODELING WITH UML63

ASSOCIATION QUALIFIED
qualifier an attribute or list of attributes whose values serve to partition the set of objects associated with an object across an association Order
1

product

* 0..1

Order line

Team

position

1..2 10

Player

an object in the source class together with a value of the qualifier uniquely select a partition in the set of target class objects use when we want to show the basis on which we partition the target set (for implementation purposes)
OO MODELING WITH UML64

COMP 211

ASSOCIATION CONSTRAINTS

ordering Country
1

Has

{ordered} *

President

subset
*

Member-of
{subset}

* *

Person

Committee

Chair-of

general C1
a..b

A
{Boolean expression}

c..d

C2

COMP 211

OO MODELING WITH UML65

ASSOCIATION ASSOCIATION CLASS


Student

[2.4.2]

Enrolled-in

Course

Enrolled-in
grade

What about an attribute like grade?


Student
A+
Marco Connie Jason

Course
COMP 332

C A B
Joe

B+ ACOMP 303

COMP 201

COMP 211

OO MODELING WITH UML66

ASSOCIATION ASSOCIATION CLASS (contd)


When to associate an attribute with an association?

salary

?
Company
name address

Person
name hkid address

Works-for

Usually needed only for many to many relationships!

COMP 211

OO MODELING WITH UML67

UML 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
flow usage realization

relates classes whose behavior or implementation affect other classes


relates two versions of an object at successive times shows that one class requires another class for its correct functioning relates a specification to its implementation (e.g., an interface to the classes that implement it)

COMP 211

OO MODELING WITH UML68

STEREOTYPE
a new class of modeling element, which is a subclass of an existing modeling element

Example:

allows the object model to be dynamically extended

we can define different kinds of classes that are useful for modeling
<<control>> <<boundary>> <<entity>>

PenTracker

OrderForm

BankAccount

PenTracker

OrderForm

BankAccount

COMP 211

special icons can be used for each stereotype


OO MODELING WITH UML69

CLASS DIAGRAM EXAMPLE


Order
date delivery address
1..*

[2.4.2]

1..*

Item
description picture cost

Payable
1 * *

Invoice
amount date due date

Has buy invoice Has sell invoice

buyer
1

Account

balance seller owner


{incomplete}

VIP
expiry date
COMP 211 OO MODELING WITH UML70

CLASS DIAGRAM EXAMPLE


Successor

Client

EventHandler
- currentEventId: integer - source: string handleRequest(): void

GUIEventHandler

COMP 211

OO MODELING WITH UML71

SUMMARY OBJECT-ORIENTED MODELING

UML Object Model Class name attribute compartment operation compartment Student
{complete, disjoint}

Undergrad

Grad

Student

10..45

Enrolled-in

1..5

Course

Course

Course-offering

COMP 211

OO MODELING WITH UML72

EXAMPLE
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 sector track

drive ASCII file

file disk

directory executable file

COMP 211

OO MODELING WITH UML73

EXAMPLE
The classes shown have attributes that are really pointers (OIDs) to other classes and which should be replaced with relationships. In the current design, a person may have up to three companies as employers. 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 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, aggregations, generalizations and association classes 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.

COMP 211

OO MODELING WITH UML74

EXAMPLE

Person name age employer 1 ID employer 2 ID employer 3 ID person ID address

Car owner ID vehicle ID owner type model year

Car loan vehicle ID customer type customer ID account number bank ID interest rate current balance

Company name company ID

Bank name bank ID

COMP 211

OO MODELING WITH UML75

You might also like