is514slide 1 is514 lecture week 7 class diagrams ii

21
IS514 Slide 1 IS514 Lecture Week 7 Class Diagrams II

Upload: justina-theodora-fowler

Post on 18-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

IS514 Slide 1

IS514 Lecture Week 7

Class Diagrams II

IS514 Slide 2

So far

• Identifying – Classes

– Attributes

– Operations

• How to draw basic class diagrams• This week

– Types of relationships

– Multiplicity

IS514 Slide 3

Relationships

• Classes do not exist in isolation• It is important to consider how they

– Interact

– Relate

• Tend to be VERBS• Five principal types of relationships

IS514 Slide 4

Type One - Dependency

• Where one class must know about another• Example:

– Prospective student knows of a course

• Not commonly used• Used more commonly in design / implementation

– Java – import javax.swing.*;

CourseProspectiveStudent knowsOf

IS514 Slide 5

Type Two - Association

• Classes associated with / related to one another• Example

– Student studies a course

• Very Commonly used• “Default” type of relationship

Student Coursestudies►

IS514 Slide 6

Type Three - Aggregation

• Classes made up of other classes• “Part of” relationship• Objects of classes that can exist in their own right• Example

– Student is a member of a seminar group

SeminarGroupStudent memberOf ►

IS514 Slide 7

Type Four - Composition

• Class is an integral part of another• “Integral part of” relationship• Object of classes that cannot exist without the other• Example:

– Seminar is delivered for a Module

Seminar ModuledeliveredFor ►

IS514 Slide 8

Type Five Inheritance

• Specialisation / generalisation between two classes• Example

– Part Time Student is a type of Student

StudentPartTimeStudent

IS514 Slide 9

Creating a Class DiagramAdding relationships

• Last week we drew a class diagram for a Cinema

• Update the diagram to include the relationship types

Filmtitleproducerlengthcertificategenre

Ticketpricetimefilm

AdultPatronnameage

ChildPatronnameage

Patronnameage

buyTicket()seeFilm()

for

of

is a is a

IS514 Slide 10

One Solution

Filmtitleproducerlengthcertificategenre

Ticketpricetimefilm

◄ for

AdultPatron

Patronnameage

buyTicket()seeFilm()

ChildPatron

of ►

IS514 Slide 11

Exercise

• A garage sells cars, accessories and car parts. The garage sells sport cars, sports utility vehicles and saloon cars.

• In groups of 3-4 spend 5 minutes – Draw the class diagram for this scenario

– Include relationships

– Include relationship types

– Ignore attributes and operations

IS514 Slide 12

One Solution

SportsUtility SportsCar SaloonCar

CarParts Car Accessoriesparts accessories

IS514 Slide 13

Exercise

• Consider Checkers game.

• Draw the class diagram for it

– Include relationships

– Include relationship types

– Ignore attributes and operations

http://www.darkfish.com/checkers/Checkers.html

IS514 Slide 14

Exercise Solution

Square

Board

64

1

WhiteChecker

WhiteSquare

RedChecker

BlackSquare

Checker

On Square

IS514 Slide 15

Alternative Naming of relationships

• Role names can be used instead of relationship names

• Write role name at either end

SquareBoard +theBoard +theSquare

IS514 Slide 16

Multiplicity

• Relationships have a multiplicity• Documents how many instances of a class may be

associated with instances of another class• Numbers placed on relationship to denote

– Minimum

– Maximum

SquareBoard1 64

+theBoard +theSquare1

IS514 Slide 17

Multiplicity

• using multiplicity we can specify :-– an exact number simply by writing it

– a range of numbers using two dots between a pair of numbers

– an arbitrary, unspecified number using a *

Department Boss11 11

ChildrenEmployee 0..*1..2 0..*1..2

Spouse0..1

1

0..1

1

IS514 Slide 18

Exercise

• Example 1

• A Student takes up to six Modules where at most 25 Students can be enrolled on each Module. A Student must study at least one Module. Modules will not run if less than 12 Students study them.

• Example 2

• An Order has to come from a single Customer and a Customer may make several Orders over time. Each of these Orders has several Order Lines, each of which refers to a single Product.

IS514 Slide 19

Exercise SolutionsExercise 1

Exercise 2

Student Module1..612..2412..24 1..6

What is missing?

Relationship namesProductOrderLine

11

Order

1..n

1

Customer11..*1..* 1

1..n

1

1 1

IS514 Slide 20

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

IS514 Slide 21

Summary

• Types of associations – Dependency– Association– Aggregation– Composition– Inheritance

• Multiplicity• Next Week – more class diagrams including

– Association Classes– Object Diagrams– Constraints– Visibility