CM402 Lecture Week 1 - Northumbria University

Download Report

Transcript CM402 Lecture Week 1 - Northumbria University

IS514 Lecture Week 7
Class Diagrams II
IS514
Slide 1
So far
• Identifying
– Classes
– Attributes
– Operations
• How to draw basic class diagrams
• This week
– Types of relationships
– Multiplicity
IS514
Slide 2
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 3
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.*;
ProspectiveStudent
IS514
knowsOf
Course
Slide 4
Type Two - Association
• Classes associated with / related to one another
• Example
– Student studies a course
• Very Commonly used
• “Default” type of relationship
Student
IS514
studies►
Course
Slide 5
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
Student
IS514
memberOf ►
SeminarGroup
Slide 6
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
IS514
deliveredFor ►
Module
Slide 7
Type Five Inheritance
• Specialisation / generalisation between two classes
• Example
– Part Time Student is a type of Student
PartTimeStudent
IS514
Student
Slide 8
Creating a Class Diagram
Adding relationships
Ticket
Film
title
producer
length
certificate
genre
for
price
time
film
• Last week we drew a
class diagram for a
Cinema
• Update the diagram to
include the relationship
types
of
AdultPatron
name
age
Patron
is a
name
age
is a
ChildPatron
name
age
buyTicket()
seeFilm()
IS514
Slide 9
One Solution
Film
title
producer
length
certificate
genre
◄ for
Ticket
price
time
film
Patron
of ►
name
age
buyTicket()
seeFilm()
AdultPatron
IS514
ChildPatron
Slide 10
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
–
–
–
–
IS514
Draw the class diagram for this scenario
Include relationships
Include relationship types
Ignore attributes and operations
Slide 11
One Solution
CarParts
parts
SportsUtility
IS514
Car
accessories
SportsCar
Accessories
SaloonCar
Slide 12
Exercise
• Consider Checkers game.
• Draw the class diagram for
it
– Include relationships
– Include relationship
types
– Ignore attributes and
operations
http://www.darkfish.com/che
ckers/Checkers.html
IS514
Slide 13
Board
Exercise Solution
1
64
Square
BlackSquare
WhiteSquare
On Square
Checker
WhiteChe
cker
IS514
RedChecker
Slide 14
Alternative Naming of relationships
• Role names can be used instead of relationship
names
• Write role name at either end
Board
IS514
+theBoard
+theSquare
Square
Slide 15
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
Board
+theBoard
1
IS514
+theSquare Square
64
Slide 16
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
Boss
1
Employee
1..2
1
0..*
Children
1
0..1
IS514
Spouse
Slide 17
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 18
Exercise Solutions
Exercise 1
Student
Module
1..6
12..24
Exercise 2
Customer
Order
1..*
What is missing?
1
1
Relationship names
1..n
OrderLine
Product
1
IS514
1
Slide 19
This weeks reading
ESSENTIAL READING
Dennis A, Wixom B, and Tegarden D (2005) System
Analysis and Design with UML version 2 second
edition, Wiley
Chapter 7
Further reading
Bennett, S., McRobb, S. and Farmer, R. (2002) ObjectOriented Systems Analysis and Design using UML,
2nd Edition, McGraw-Hill
Pages 168-176
http://www.agilemodeling.com/artifacts/classDiagram.htm
http://www.omg.org
IS514
Slide 20
Summary
• Types of associations
–
–
–
–
–
Dependency
Association
Aggregation
Composition
Inheritance
• Multiplicity
• Next Week – more class diagrams including
–
–
–
–
IS514
Association Classes
Object Diagrams
Constraints
Visibility
Slide 21