22
IS0514 Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

Embed Size (px)

Citation preview

Page 1: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 1

IS0517 Lecture Week 8

Class Diagrams III

Page 2: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 2

So far• Identifying

– Classes• Attributes• Operations

– Types of relationships– Multiplicity

• How to draw class diagrams• This week

– Class Diagrams• Visibility• Association Classes• Constraints• Recursive relationships

– Object Diagrams

Page 3: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 3

Visibility• Quick Question:

– Are all attributes available to other objects?

– Consider the class person

– Which attributes should be available?

– Which operations should be available?

• Attributes– Probably many of them are private

• Operations– Probably most of them are public

PersonhairColourageheightweight

askAge()askHeight()askWeight()askHairColour()

Page 4: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 4

Visibility in UML

Visibility Description Symbol

Public Is directly accessible by an instance of any class

+

Private May only be used by an instance of the class that includes it

-

Protected May be used either by an instance of the class that includes it or by a subclass of that class

#

Package Is directly accessible only by instances of a class in the same package

~

Design

Analysis

Page 5: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 5

Examples

Person- hairColour- age- height- weight

+ askAge()+ askHeight()+ askWeight()+ askHairColour()

Student- name- age

+ takesNotes()+ doHomework()+ sitExam()+ gotoLibrary()+ gotoStudentUnion()+ getName()+ getAge()

Page 6: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 6

Exercise 1

• Given the class BankAccount below• Identify the visibility of the (i.e.,

choose + or – ) for:– Attributes– Operations

BankAccountaccountHolderaccountNumberbalance

getAccountHolder()getAccountNumber()getBalance()setBalance()withdraw()deposit()

Page 7: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 7

Exercise 1 – One Answer

BankAccount- accountHolder- accountNumber- balance

+ getAccountHolder()+ getAccountNumber()+ getBalance()- setBalance()+ withdraw()+ deposit()

I would love to be able to set the balance on my account but can I?

Page 8: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 8

Association Classes• Sometimes the way in which objects of a class are associated is just as

important as the objects of a class themselves

• Consider the association between Student and Module. Where should our system record the student’s marks on for each student on each module?

Student Module1..* 6is taking

is takingmark : Integer

associationclass

Page 9: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 9

Exercise 2 – From Seminar• A publisher specialises in producing volumes of short stories.

Most stories are by one author, but occasionally, two authors collaborate on a story. Stories that have been originally published in one volume may be re-issued in other collections. Whenever a story is used in a volume, various operational details must be recorded. Stories have a title and a genre. Authors have a name and an address. Collections have a title and an introduction. When a story is used in a volume the position in the volume needs to be recorded.

• Redraw including– Multiplicity

– Association Class

Page 10: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 10

Exercise 2 -One Solution

Authornameaddress

StoryUserank

Storytitlegenre

1..*

1..2writes

Collectiontitleintroduction1..*1..*

used in

1..*

1..2

1..*1..*

Page 11: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 11

Constraints

• a constraint is a condition that has to be satisfied • Use with care

– Too much text on a diagram

– Can be over used

OrderdateReceivedisPrepaidnumber: Stringprice: Money

dispatch()close()

Order Line

quantity : Integerprice : MoneyisSatisfied : Boolean

*line

items

1

{if Order.customer.creditRatingis “poor”, then Order.isPrepaidmust be true}

Customer

creditRating

getCreditRating

1..*

1

Page 12: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 12

Constraints... continued

• another common situation is when there is an ‘exclusive or’ between two associations

Copy Book

Journal

1..*is a copy of

0..1

is a copy of

1..*

0..1

Copy Book

Journal

1..* is a copy of 0..1

is a copy of

1..*

0..1

{or}

under-constrained Using an or-constraint

Page 13: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 13

Exercise 3

• Consider a car rental company• The company will only rent a car to customers over

the age of 25

RentalCar Customerage

0..*1..* 0..*

rents

1..*

Page 14: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 14

Exercise 3 – One Solution

RentalCar Customerage

0..*1..* 0..*

rents

1..* {customer.age > 25}

Page 15: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 15

Exercise 4

• Draw a very simple class diagram showing the relationship between children and their biological parents. Don’t worry about attributes etc.

• Can you represent the same relationship using only one class.

Page 16: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 16

Exercise 4 – One solution

FamilyMember0..2

0..14+child

0..14

+parent

0..2

Page 17: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 17

Recursive Association- is allowed, and can be useful !

• an employee, as a manager, may manage other employees; each employee may be managed by another employee

employee

manages

0..*

0..1

Page 18: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 18

Class v. Object Diagram

Staff

staffNamestaffIDstaffstartdatestaffdept

AkhtarAli:Staff

staffName=Akhtar Ali staffID=313staffstartdate=140800staffdept=SDE

full symbol for an object is a box with 3 compartments• class name• attributes• operations

an instance symbol shows bothinstance and class name underlinedwith a colon to separate

in an instance symbol, both attributenames and values are given

Page 19: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 19

Exercise 5

• Given the class diagram• Draw the object diagram given the following

instances– Ford Fiesta £4999

– Subaru Impreza WRD £22000 with a flash stereo

– Hyundai Accent £7995 with child seats

Accessoriesname

Carmakemodelprice 0..* 0..*

accessories

0..*0..*

Page 20: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 20

Exercise 5 – One Answer

make = Fordmodel = Fiestaprice = £4999

ThisFordFiesta : Car

make = Subarumodel = Imprezaprice = £22000

thisSubaruImpreza : Car

make = Hyundaimodel = Accentprice = £7999

thisHydundaiAccent : Car

name = stereostereo : Accessorycar accessory

name = Child Seatschildseats : Accessorycar accessory

Page 21: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 21

This weeks reading

ESSENTIAL READINGDennis A, Wixom B, and Tegarden D (2005) System

Analysis and Design with UML version 2 second edition, Wiley

Chapter 7Further readingBennett, S., McRobb, S. and Farmer, R. (2002) Object-

Oriented Systems Analysis and Design using UML, 2nd Edition, McGraw-Hill

Pages 168-176http://www.agilemodeling.com/artifacts/classDiagram.htm http://www.omg.org

Page 22: IS0514Slide 1 IS0517 Lecture Week 8 Class Diagrams III

IS0514 Slide 22

Summary

• Visibility• Association classes• Constraints• Recursive relationships• Object diagrams• Next week

– Behavioural Model

– Class Responsibility Collaboration Cards