79
OO MODELING WITH UML OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

Embed Size (px)

Citation preview

Page 1: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML1

OBJECT-ORIENTED MODELING WITH OBJECT-ORIENTED MODELING WITH UMLUML

OBJECT-ORIENTED MODELING WITH OBJECT-ORIENTED MODELING WITH UMLUML

SOFTWARE ENGINEERINGSOFTWARE ENGINEERINGSOFTWARE ENGINEERINGSOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML2

OBJECTIVES LEARNING

1 Understand wh a t is th e UML a n d h ow it ca n b e u s e d to m sssss sss ssssss ss

2 Unde r s t a nd t he m os t e s s e n t ia l fe a tu re s of UML cla s s d ia g ra m s sss sssssssssssss

3 Know s ome mor e a d va n ce d fe a tu re s of th e UML a g g re g a t io n a n d con s t ra in t s

4 Appr e c i a t e t ha t t he UML i s a mode l i ng l a ngua ge a nd nos s

ssssssss sss s sssssssssss

OO MODELING WITH UMLOO MODELING WITH UML3

OBJECT-ORIENTED MODELING WITH UML OUTLINEOBJECT-ORIENTED MODELING WITH UML OUTLINE

Overview of UML

Modeling and Abstractionndash OO modelingndash aggregation classification and generalization

Objects and Classesndash state amp behaviourndash attributes amp operations

Generalizationndash inheritancendash coverage

Links and Associationsndash multiplicityndash aggregation amp composition

2

OO MODELING WITH UMLOO MODELING WITH UML4

WHAT IS UMLWHAT IS UML

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 premisebasic premise software systems can be modeled ascollections of collaborating objects

22

OO MODELING WITH UMLOO MODELING WITH UML5

UML STRUCTUREUML STRUCTURE

Building blocksndash thingsndash relationshipsndash diagrams

Common mechanismsndash specificationsndash adornmentsndash common divisionsndash extensibility mechanisms

Architecturendash use-case view

ndash logical view

ndash process view

ndash implementation view

ndash deployment view

UML

Common mechanisms

ArchitectureBuilding blocks

OO MODELING WITH UMLOO MODELING WITH UML8

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why modelsndash Models succinctly describe reality (ie they abstract reality)

show essential details filter out non-essential details

ndash This allows us to focus on the ldquobig picturerdquo of software

development ie programming-in-the-large

ndash and thus to better deal with the complexity of software

development with human limitations in understanding complex things

ndash resulting in better understanding of requirements cleaner designs

and more maintainable systems

23

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 2: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML2

OBJECTIVES LEARNING

1 Understand wh a t is th e UML a n d h ow it ca n b e u s e d to m sssss sss ssssss ss

2 Unde r s t a nd t he m os t e s s e n t ia l fe a tu re s of UML cla s s d ia g ra m s sss sssssssssssss

3 Know s ome mor e a d va n ce d fe a tu re s of th e UML a g g re g a t io n a n d con s t ra in t s

4 Appr e c i a t e t ha t t he UML i s a mode l i ng l a ngua ge a nd nos s

ssssssss sss s sssssssssss

OO MODELING WITH UMLOO MODELING WITH UML3

OBJECT-ORIENTED MODELING WITH UML OUTLINEOBJECT-ORIENTED MODELING WITH UML OUTLINE

Overview of UML

Modeling and Abstractionndash OO modelingndash aggregation classification and generalization

Objects and Classesndash state amp behaviourndash attributes amp operations

Generalizationndash inheritancendash coverage

Links and Associationsndash multiplicityndash aggregation amp composition

2

OO MODELING WITH UMLOO MODELING WITH UML4

WHAT IS UMLWHAT IS UML

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 premisebasic premise software systems can be modeled ascollections of collaborating objects

22

OO MODELING WITH UMLOO MODELING WITH UML5

UML STRUCTUREUML STRUCTURE

Building blocksndash thingsndash relationshipsndash diagrams

Common mechanismsndash specificationsndash adornmentsndash common divisionsndash extensibility mechanisms

Architecturendash use-case view

ndash logical view

ndash process view

ndash implementation view

ndash deployment view

UML

Common mechanisms

ArchitectureBuilding blocks

OO MODELING WITH UMLOO MODELING WITH UML8

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why modelsndash Models succinctly describe reality (ie they abstract reality)

show essential details filter out non-essential details

ndash This allows us to focus on the ldquobig picturerdquo of software

development ie programming-in-the-large

ndash and thus to better deal with the complexity of software

development with human limitations in understanding complex things

ndash resulting in better understanding of requirements cleaner designs

and more maintainable systems

23

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 3: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML3

OBJECT-ORIENTED MODELING WITH UML OUTLINEOBJECT-ORIENTED MODELING WITH UML OUTLINE

Overview of UML

Modeling and Abstractionndash OO modelingndash aggregation classification and generalization

Objects and Classesndash state amp behaviourndash attributes amp operations

Generalizationndash inheritancendash coverage

Links and Associationsndash multiplicityndash aggregation amp composition

2

OO MODELING WITH UMLOO MODELING WITH UML4

WHAT IS UMLWHAT IS UML

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 premisebasic premise software systems can be modeled ascollections of collaborating objects

22

OO MODELING WITH UMLOO MODELING WITH UML5

UML STRUCTUREUML STRUCTURE

Building blocksndash thingsndash relationshipsndash diagrams

Common mechanismsndash specificationsndash adornmentsndash common divisionsndash extensibility mechanisms

Architecturendash use-case view

ndash logical view

ndash process view

ndash implementation view

ndash deployment view

UML

Common mechanisms

ArchitectureBuilding blocks

OO MODELING WITH UMLOO MODELING WITH UML8

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why modelsndash Models succinctly describe reality (ie they abstract reality)

show essential details filter out non-essential details

ndash This allows us to focus on the ldquobig picturerdquo of software

development ie programming-in-the-large

ndash and thus to better deal with the complexity of software

development with human limitations in understanding complex things

ndash resulting in better understanding of requirements cleaner designs

and more maintainable systems

23

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 4: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML4

WHAT IS UMLWHAT IS UML

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 premisebasic premise software systems can be modeled ascollections of collaborating objects

22

OO MODELING WITH UMLOO MODELING WITH UML5

UML STRUCTUREUML STRUCTURE

Building blocksndash thingsndash relationshipsndash diagrams

Common mechanismsndash specificationsndash adornmentsndash common divisionsndash extensibility mechanisms

Architecturendash use-case view

ndash logical view

ndash process view

ndash implementation view

ndash deployment view

UML

Common mechanisms

ArchitectureBuilding blocks

OO MODELING WITH UMLOO MODELING WITH UML8

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why modelsndash Models succinctly describe reality (ie they abstract reality)

show essential details filter out non-essential details

ndash This allows us to focus on the ldquobig picturerdquo of software

development ie programming-in-the-large

ndash and thus to better deal with the complexity of software

development with human limitations in understanding complex things

ndash resulting in better understanding of requirements cleaner designs

and more maintainable systems

23

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 5: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML5

UML STRUCTUREUML STRUCTURE

Building blocksndash thingsndash relationshipsndash diagrams

Common mechanismsndash specificationsndash adornmentsndash common divisionsndash extensibility mechanisms

Architecturendash use-case view

ndash logical view

ndash process view

ndash implementation view

ndash deployment view

UML

Common mechanisms

ArchitectureBuilding blocks

OO MODELING WITH UMLOO MODELING WITH UML8

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why modelsndash Models succinctly describe reality (ie they abstract reality)

show essential details filter out non-essential details

ndash This allows us to focus on the ldquobig picturerdquo of software

development ie programming-in-the-large

ndash and thus to better deal with the complexity of software

development with human limitations in understanding complex things

ndash resulting in better understanding of requirements cleaner designs

and more maintainable systems

23

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 6: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML8

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why modelsndash Models succinctly describe reality (ie they abstract reality)

show essential details filter out non-essential details

ndash This allows us to focus on the ldquobig picturerdquo of software

development ie programming-in-the-large

ndash and thus to better deal with the complexity of software

development with human limitations in understanding complex things

ndash resulting in better understanding of requirements cleaner designs

and more maintainable systems

23

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 7: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML9

OBJECT-ORIENTED MODELINGOBJECT-ORIENTED MODELING

Why objects

ndash Objects more accurately reflect the real world in a model thereby

reducing the ldquosemantic gaprdquo between reality and a model

ndash Objects localize changes to the model

23

We model reality as a number of interacting objects

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 8: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML10

OOMODELING amp LEVELS OF ABSTRACTION

Conceptual Level- We construct a problem domain modelndash We do not consider any aspects of implementation of objects

Focus on identifying concept(ldquoobjectsrdquo)

in the problem domain

Design Level - We construct a solution domain modelndash We consider interfaces of objects (but no internal aspects)

Focus on how objects interact in the solution domain

Implementation Level - We implement the solution domain modelndash We 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

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 9: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML11

- wings

ABSTRACTIONABSTRACTION

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

mental process of mental process of selecting someselecting some object characteristics and object characteristics and properties to model and properties to model and excluding othersexcluding others that are not relevant that are not relevant

landing gear 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

flies

232

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 10: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML12

TYPES OF ABSTRACTIONSTYPES OF ABSTRACTIONS

classification mdash group similar instances of objects

DBMSsoftware

Sybase Access Oracle

IS_MEMBER_OFrelationship

pick out common properties and ignore unique properties

Microsoftsoftware

Word PowerPoint

IS_MEMBER_OFrelationship

232

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 11: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML13

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

aggregation mdash group dissimilar sets of objects

airplane

fuselage wing landing gear

IS_PART_OFrelationship

ignore differences among the parts and concentrateon the fact that they form the whole

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 12: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML14

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

generalization mdash group similar sets of objects

student

postgrad secondary undergrad

IS_Arelationship

note difference between classification and generalization

ndash classification ndash applied to individual instances of objects

ndash generalization ndash applied to sets of objects (classes)

superclasssupertype

subclasssubtype

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 13: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML15

TYPES OF ABSTRACTIONS (contrsquod)TYPES OF ABSTRACTIONS (contrsquod)

student

undergrad

John Sam Tina

name student id

generalizationgeneralization

aggregationaggregation

classificationclassification

Note we can combine different types of abstraction when modeling

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 14: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML16

OBJECTOBJECT

The interface describes the operationswhich an object can perform

Fredrsquos SavingsAccount(a real-life object)

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

a concept abstraction or thing with a concept abstraction or thing with crisp crisp boundariesboundaries and and meaningmeaning which plays a role which plays a role with respect to a with respect to a requestrequest for an for an operationoperation

balance

deposit

withdraw

234 242

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 15: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML17

Balance

Deposit

Withdraw

OBJECT mdash PROPERTIESOBJECT mdash PROPERTIES

Fredrsquos SavingsAccount

accountNumber (integer value)

amount (money value)

state

balance

Balance Method (code)

deposit

Deposit Method (code)

withdraw

Withdraw Method (code)

payInterest

Pay-interest Method (code)

behaviour

identity

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 16: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML19

OBJECT PROVIDE INFORMATION HIDINGOBJECT PROVIDE INFORMATION HIDING

An objectrsquos interface encapsulates and abstracts an object thereby providing information hiding

An object has a visible ( public) interface and a hidden ( private) implementation

object 1

interface

response

request

user

request

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

a kind of abstraction that enforces a (clean) a kind of abstraction that enforces a (clean) separation between the separation between the external interfaceexternal interface

and the and the internal implementationinternal implementation of an objectof an object

response

object 2

interface

Objects help promote modular development

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 17: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML20

(OBJECT) CLASS(OBJECT) CLASS

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

a description of a a description of a group of objectsgroup of objects that have common that have commonndash statestate ndashndash relationshipsrelationshipsndash behaviourbehaviour ndashndash semanticssemantics

A class provides a template to create objects (a ldquofactoryrdquo for objects)

a class is a classifierclassifier an object is an instanceinstance

A good class should capture only one abstraction

It should one major theme

A class should be named using the vocabulary of the problem domainSo that it is meaningful and traceable

234 242

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 18: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML21

WHY CLASSESWHY CLASSES

234 242

conveniently describes a collection of objects abstraction

reduces complexity ndash makes systems easier to understand

write-once ndash common definition defined and stored once

specifies what values are acceptable in a given context

allows compilers (and people) to understand the programmerrsquos intention and to do certain types of error checking

Choosing appropriate classes is anChoosing appropriate classes is animportant design decisionimportant design decision

How are classes and modules related

ndash classes can be thought of asloosely coupled highly cohesive modules

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 19: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML22

CLASS mdash ATTRIBUTECLASS mdash ATTRIBUTE

each attribute has andash name (unique within a class but not across classes)

a description of a description of data valuesdata values held by objects in a class held by objects in a classa description of a description of data valuesdata values held by objects in a class held by objects in a class

ndash type (specifies the domain of values ndash string integer money etc ndash from which an attribute can take its values) continuous (eg salary) discrete (eg sex)

ndash visibility public (+) private (-) protected () package (~)

ndash initial value (optional)

ndash multiplicity (optional number of simultaneous valueseg telephone number default is exactly 1)

ndash changeability frozen addOnly no constraint (default)

base attribute versus derived attributendash eg birthdate versus age

234

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 20: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML23

ATTRIBUTES mdash UML NOTATIONATTRIBUTES mdash UML NOTATION

laquostereotyperaquo visibility name [multiplicity] typeExpression = initialValue propertyString

propertyString ndashgt a comma separated list of properties or constraints

only ldquonamerdquo is mandatory

Examples

+ size area = (100000) frozen

name string

telephone[02] string

telephone[1 34] string

- salary money gt0 lt1000000

You are not required to know this syntax

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 21: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML24

CLASS mdash OPERATIONCLASS mdash OPERATION

Company hire fire pay-dividends hellip

Course register waive-prerequisite hellip

An operation invoked by a message sent to an object

ndash operation signature name of operation (called selector) number and types of the arguments and type of the result value

ndash visibility public (+) private (-) protected () package (~)

ndash side effectsif the execution of an operation changes the state of an object (query operation no side effects)

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

a a functionfunction or or transformationtransformation that may that may be applied to or by objects in a classbe applied to or by objects in a class

234 242

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 22: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML25

CLASS mdash METHODCLASS mdash METHOD

polymorphic operation ndash an operation that can have several different methods

class operation

Account payInterest

Savings calculate interest on savings accountsChecking calculate interest on checking accounts

The The implementationimplementation of an operation for a class of an operation for a classThe The implementationimplementation of an operation for a class of an operation for a class

dynamic binding ndash choosing the method to execute for an operation based on the objectrsquos class

An operation is visible the method is hidden

ndash An operation is a classifier a method is an instance

242

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 23: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML26

OPERATION mdash UML NOTATIONOPERATION mdash UML NOTATION

laquostereotyperaquo visibility name (parameterList) returnType propertyString

parameterList ndashgt 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 ndashgt 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

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 24: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML27

CLASS mdash UML GRAPHICAL NOTATIONCLASS mdash UML GRAPHICAL NOTATION

Account

-accoutNumber int+amount money-count int = 0

+create(aNumber int)-incrementCount( )+getCount( ) int+getNumber( ) int+balance( ) money+deposit(amount)+withdraw(amount)-payInterest( )

Visibility+ public- private protected~ package

Class nameattribute

compartment

operationcompartment

extracompartment

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 25: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML28

SCOPESCOPE

Attributes and operations normally have instance scope(ie they apply tooperate on a single object instance)

Sometimes it is useful to have attributes and operations that have class scope (ie they apply tooperate on an entire class of objects) attributes whose values are class determined ndash a counter object constructors ndash create new object instances

In UML notation such attributesoperations are underlined

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 26: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML29

WHY CLASSES FOR MODELING SYSTEMS

By sssssssssss a colle c t ion of ob je c t s a n d re p re s e n t in g t ss s ssssss sss com p le xity of d e ve lop in g a s ys te m is re

duced s ince it becomes eas ie r tondash understand the system We need to understand only the

classes not the individual objects

ndash specify the system Classes provide a place to define and

store common definitions only once

Choosing appropriate classes is an Choosing appropriate classes is an

IMPORTANT DESIGN DECISION IMPORTANT DESIGN DECISION

that helps promote modular development that helps promote modular development

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 27: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML30

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 28: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML31

GENERALIZATIONGENERALIZATION

a a relationshiprelationship between a between a classclass of kind of kinda a relationshiprelationship between a between a classclass of kind of kind

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

242

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 29: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML32

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

Can also be applied bottom-up

242

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 30: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML33

GENERALIZATION SPECIALZATIONGENERALIZATION SPECIALZATION

242

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 31: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML34

a a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versionsa a relationshiprelationship between a between a classclass and one or more and one or more refined versionsrefined versions

GENERALIZATIONGENERALIZATION

Vehicle

Van Car Truck

Compact Midsize Fullsize

car-sizediscriminator

vehicle-type

generalizationgeneralization

specializationspecialization

discriminator an attribute of enumeration type that indicates which property of a class is being abstracted by a generalization

subclass

superclass

superclass subclass

242

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 32: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML35

GENERALIZATION mdash INHERITANCEGENERALIZATION mdash INHERITANCE

We extract similarities (common attributes and operations) put them in a superclass and inherit them to subclass(es)

1048578 Properties and behaviour are only defined in one placeradic reduces redundancy of descriptionsradic promotes reusability of descriptionsradic simplifies modification of descriptions

An object of a superclass can be substituted

with an object of the subclass

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

the the assumption of propertiesassumption of properties by a by a subclasssubclass from its from its superclassessuperclasses

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 33: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML36

payInterest

interestRate

GENERALIZATION mdash INHERITANCE (contrsquod)GENERALIZATION mdash INHERITANCE (contrsquod)

Checking Savings

AccountaccountNumberamount

balancedepositwithdraw

serviceCharge

withdraw

CheckingSavings

conflict resolution byndash user-defined orderndash predefined orderndash redefine names

(attributes only)

A subclass mayndash add new properties

(attributes operations)

ndash override methods

Which withdraw method should CheckingSavings inherit

Multiple inheritance

Single inheritance

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 34: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML37

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Student Employee

Undergrad Postgrad

TA

Staff Faculty

UniversityMember

id

id id

id id id id

id id

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 35: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML38

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

id id id id

id id

Student Employee

Undergrad Postgrad

TA

Staff Faculty

idid

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 36: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML39

MULTIPLE INHERITANCE EXAMPLEMULTIPLE INHERITANCE EXAMPLE

Vehicle

Car AmphibiousVehicle

Boat

LandVehiclesize

WaterVehiclesize

weight displacement

name conflict

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 37: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML40

GENERALIZATION mdash ABSTRACT CLASSGENERALIZATION mdash ABSTRACT CLASS

An abstract class used for modeling purposes as a container for definitions but no instances of the class are of interest

Checking Savings

Account

Note Operations may also be abstract no method specifiedShown in UML by using italics

a class that has a class that has no direct instancesno direct instancesa class that has a class that has no direct instancesno direct instances

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 38: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML41

Person

Fem

ale

Player

Mal

eGENERALIZATION mdash COVERAGEGENERALIZATION mdash 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

Tennis Soccer

Player

overlapping

Male Female

Person

disjoint

Ten

nis

Soccer

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 39: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML42

UniversityStudent

Postgrad

Tree

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

incomplete - some superclass object is not a member of any subclass

complete - all superclass objects are also members of some subclass

Oak BirchElm

Tree

incomplete

PostgradUndergrad

UniversityStudent

complete

Un

der

grad

Oak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 40: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML43

Player

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Tennis Soccer

Player

overlapping incomplete

UG PG

Course

overlapping complete

overlapping incomplete

overlapping complete

Ten

nis

Soccer

Course

UG

PG

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 41: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML44

UniversityStudentP

ostgrad

GENERALIZATION mdash COVERAGE (contrsquod)GENERALIZATION mdash COVERAGE (contrsquod)

Oak BirchElm

Tree

disjoint incomplete

PostgradUndergrad

UniversityStudent

disjoint complete

disjoint complete

disjoint incomplete

Un

der

grad

TreeOak

Elm

Birch

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 42: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML45

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

Cash Debit-cardCredit-card

Payment

What is the policy of the organization

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 43: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML46

The coverage of the generalization shown above is

ndash overlapping complete

ndash disjoint complete

ndash overlapping incomplete

ndash disjoint incomplete

QUESTIONQUESTION

What is the policy of the organization

Individual Company

Customer

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 44: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML47

a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances a physical or conceptual a physical or conceptual relationship between object instancesrelationship between object instances

LINKLINK

TeachesWorks-for Beat Wuumlthrich

Pam Drew

Fred Lochovsky

Gan Kok Wee

Dik Lee

Person

HKUST

Boeing

Organization

COMP 111

COMP 337

COMP 211

COMP 332

Course

links allow one object to send messages to another object

242

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 45: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML48

An association is a classifierclassifier a link is an instanceinstance

Works-forPerson Organization

ASSOCIATIONASSOCIATION

a a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semanticsa a descriptiondescription of a of a group of linksgroup of links with with common semanticscommon semantics

Person Course

Conceptually associations are inherently bi-directional

Teaches

Works-for

Can show navigability ofassociations(implies that the source object has a reference to the target object)

Can show readability ofassociations

classclassdiagramdiagram

W

orks-for

Organization

Teaches

242

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 46: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML49

ASSOCIATION (contrsquod)ASSOCIATION (contrsquod)

there can be several associations between the same two classes

Person UniversityAttended

Works-for

Fred bull bull U Toronto

PersonManagesMarried-to

or even with the same class

Attended

Works-for

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 47: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML50

ASSOCIATION mdash ASSOCIATION mdash DEGREEDEGREE

unary (reflexive)relates a class to itself Person

Manages

binaryrelates two classes

Person Course Teaches

ternaryrelates three classes

Project Language

Person n-ary - relates any number of classes

In practice the vast majority of associations are binary

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 48: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML51

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Customer Account

Bank

can this be expressed as two binary associationssuppose we know John has account 532 with HSBC

John has account 121 with BoCMary has account 993 with BoC

Customer BankAccountHas Is-with

Bank AccountCustomerIs-with Has

Customer AccountBankIs-with Has

X

X

radic

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 49: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML55

ASSOCIATION mdash DEGREE (contrsquod)ASSOCIATION mdash DEGREE (contrsquod)

Project Language

Person

can this be expressed as two binary associationssuppose we know Mary works on the Accounting project using Cobol

Mary works on the CAD project using CJohn works on the Accounting project using C

Project PersonLanguageWith Uses

Person LanguageProjectWorks-on With

Project LanguagePersonWorks-on Uses

X

X

X

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 50: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML60

ASSOCIATION mdash MULTIPLICITYASSOCIATION mdash MULTIPLICITY

Instructor CourseTeaches 01

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

For a given course how many instructors can teach it

For a given instructor how many courses can he teach

a real-world constraint

each course must be taught by one instructor but may have many

an instructor does not have to teach any course but may teach more than one course in a given semester

242 532

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 51: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML61

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

minimal cardinality (min-card)

min-card(C1A) minimum number of links in which each object of C1 can participate in A

C1 C2

A cdab

min-card(C2A)min-card(C1A)

maximal cardinality (max-card)

max-card(C1A) maximum number of links in which each object of C1 can participate in A

max-card(C2A)max-card(C1A)

min-card(C1A) = 0 optional participation

min-card(C1A) gt 0 mandatory participation

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 52: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML62

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

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(C1A) = 1 and max-card(C2A) = 1 one-to-one association (11)

max-card(C1A) = 1 and max-card(C2A) = one-to-many association (1N)

max-card(C1A) = and max-card(C2A) = many to many association (NM)

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 53: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML63

01

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

one-to-one association (11)

111

Country

China

Canada

USA

City

Beijing

Ottawa

New York

Washington

Capital-of

Country CityCapital-of

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 54: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML64

one-to-many association (1N)

1 110

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Assigned-toEmployee

John

Alan

Bill

Larry

James

Employee DepartmentAssigned-to

Department

Sales

Marketing

Production

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 55: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML65

many to many association (NM)

ASSOCIATION mdash MULTIPLICITY (contrsquod)ASSOCIATION mdash MULTIPLICITY (contrsquod)

Supplier PartSupplies

Supplier

E-com

SGH

Hi-tech

Lansing

Mach

Vtech

Part

85213

28175

33472

90231

Supplies

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 56: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML66

1045 15

QUESTIONQUESTION

Student CourseEnrolled-in

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

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 57: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML67

N-ARY ASSOCIATION mdash MULTIPLICITYN-ARY ASSOCIATION mdash MULTIPLICITY

defined with respect to the defined with respect to the otherother n-1 ends n-1 endsdefined with respect to the defined with respect to the otherother n-1 ends n-1 ends

for each (course student)

01

for each (student professor)

for each (course professor)

Student

Course Professor

Registration

0 or 1 professor

many courses

many students

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

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

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 58: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML68

QUESTIONQUESTION

03

01

0 1 2 or 3 days

0 or 1 classrooms

many courses

for each (course classroom)

for each (course day)

for each (classroom day)

Classroom

Course Day

Meets

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 59: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML69

What is the most likely multiplicity of the following associations

QUESTIONQUESTION

ServiceOrder ServiceCallHas

ServiceAgent ServiceOrderTakes

ServiceOrder PartWaitsFor

Product PartComposedOf

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 60: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML70

EXAMPLE EXAMPLE CAR LOANSCAR LOANS

The classes shown have some attributes that are internal object identifiers(OIDs) that should not appear at the conceptual modeling level and should either be deleted or be replaced with relationships All such attributes conveniently have names ending in ID

Some further information about the application follows A person may work for any number of companies Persons companies or banks may own cars The car owner ID represents either 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 is for the purchase of a car A car may have multiple car loans

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

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 61: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML71

EXAMPLEEXAMPLE

Person

nameageemployer 1 IDemployer 2 IDemployer 3 IDperson IDaddress

Car

owner IDvehicle IDowner typemodelyear

Car loan

vehicle IDcustomer typecustomer IDaccount numberbank IDinterest ratecurrent balance

Company

namecompany ID

Bank

namebank ID

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 62: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML72

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 63: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML73

AGGREGATIONCOMPOSITION ASSOCIATIONAGGREGATIONCOMPOSITION ASSOCIATION

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

DiskComputerHas

a special type of association in which there is a ldquopart-ofrdquo relationship between one class and another class

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

Course-offeringCourseHas

242

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 64: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML74

AGGREGATIONCOMPOSITION (contrsquod)AGGREGATIONCOMPOSITION (contrsquod)

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

CarChassis Engine

Doors FrameTransmission

Which aggregation association to use is a matter of how one interprets reality andor the requirements of the application

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 65: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML75

WHEN TO USE AGGREGATIONCOMPOSITIONWHEN TO USE AGGREGATIONCOMPOSITION

Would you use the phrase ldquopart ofrdquo to describe the association or name it ldquoHasrdquo (But be CAREFUL Not all ldquoHasrdquo 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)

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 66: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML76

ASSOCIATION mdash ROLE NAMESASSOCIATION mdash ROLE NAMES

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

Person Company Works-foremployee employer

for binary associations there are two roles

242

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 67: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML77

ASSOCIATION mdash ROLE NAMES (contrsquod)ASSOCIATION mdash ROLE NAMES (contrsquod)

Boss

Worker

PersonManages

John

Sarah

Frank

Bill

Boss

Boss

Worker

It is necessary to use role names when an associationrelates objects from the same class

rolerole one end of an association one end of an associationrolerole one end of an association one end of an association

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 68: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML78

What about an attribute like grade

ASSOCIATION mdash ASSOCIATION CLASSASSOCIATION mdash ASSOCIATION CLASS

Student Course Enrolled-in

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

A+

B+

A-C

A

B

242

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 69: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML79

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 1 use many attributes for grade

Enrolled-inStudent Course

studentnamegrade1grade2

Student Course Enrolled-in

course-codenamegrade1grade2

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 70: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML80

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Possibility 2 use a multi-valued attribute for grade

Enrolled-inStudent Course

studentnamegrade [1]

Student Course Enrolled-in

course-codenamegrade [1]

(a) in Student

(b) in Course

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 71: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML81

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

Student Course

Enrolled-ingrade

Course

COMP 332

COMP 201

COMP 303

Student

Marco

Connie

Jason

Joe

Possibility 3 use an association class

B+

A-C

A

B

A+

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 72: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML82

ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)ASSOCIATION mdash ASSOCIATION CLASS (contrsquod)

When to associate an attribute with an association

Usually needed only for many to many relationships

Person Companynamehkidaddress

nameaddress

Works-for

salary

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 73: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML83

ASSOCIATION mdash CONSTRAINTSASSOCIATION mdash CONSTRAINTS

ordering

subset

1Person CommitteeMember-of

subset

Chair-of

general

Boolean expressioncdabC1 C2

A

1Country President

Has ordered

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 74: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML84

UML RELATIONSHIPSUML 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(eg an interface to the classes that implement it)

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 75: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML85

STEREOTYPESTEREOTYPE

allows the object model to be dynamically extended

Example we can define different kinds of classes that are useful for modeling

laquoboundaryraquo

OrderFormlaquoentityraquo

BankAccountlaquocontrolraquo

PenTracker

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

a a new classnew class of modeling element which is a of modeling element which is a subclasssubclass of an existing modeling element of an existing modeling element

special icons can be used for each stereotype

OrderForm BankAccountPenTracker

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 76: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML86

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Invoiceamountdatedue date

Order

datedelivery address

Payable

Account

balanceowner

Itemdescriptionpicturecost

VIP

expiry date

1

1

1

1

1Has buy invoice

Has sell invoice

incomplete

buyer

seller

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 77: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML87

CLASS DIAGRAM EXAMPLECLASS DIAGRAM EXAMPLE

Client

Successor

GUIEventHandler

EventHandler

- currentEventId integer- source string

handleRequest() void

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 78: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML88

EXAMPLEEXAMPLE

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

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link

Page 79: OO MODELING WITH UML 1 OBJECT-ORIENTED MODELING WITH UML SOFTWARE ENGINEERING SOFTWARE ENGINEERING

OO MODELING WITH UMLOO MODELING WITH UML89

SUMMARY mdash OBJECT-ORIENTED MODELINGSUMMARY mdash OBJECT-ORIENTED MODELING

UML mdash Object Model

Class nameattributecompartment

operationcompartment

GradUndergrad

Student

complete disjoint

Course-offeringCourse

151045Student CourseEnrolled-in

classifier instance class object

attribute valueoperation method

association link