these slides are not to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 class...

28
WATERLOO CHERITON SCHOOL OF COMPUTER SCIENCE Introduction to Introduction to Unified Modelling Language (UML) Unified Modelling Language (UML) (part 2- Class diagram, Associations) (part 2- Class diagram, Associations) CS 446/646 ECE452 May 6 th , 2011 Material covered in this lecture is based on various chapters from UML 2 and the Unified Process- 2 nd  Edition Practical Object Oriented Analysis & Design IMPORTANT NOTICE TO STUDENTS These slides are NOT to be used as a replacement for student notes. These slides are sometimes vague and incomplete on purpose to spark a class discussion

Upload: others

Post on 18-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Introduction to Introduction to Unified Modelling Language (UML)Unified Modelling Language (UML)

(part 2­ Class diagram, Associations)(part 2­ Class diagram, Associations)

CS 446/646 ECE452May 6th, 2011

Material covered in this lecture is based on various chapters from UML 2 and the Unified Process­ 2nd Edition Practical Object Oriented Analysis & Design

IMPORTANT NOTICE TO STUDENTS

These slides are NOT to be used as a replacement for student notes.These slides are sometimes vague and incomplete on purpose to spark a class discussion

Page 2: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 2WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

ObjectsWhat are Objects● “a discrete entity with a well-defined boundary that

encapsulates state and behaviour; an instance of a class” UML Reference Manual, 2nd Edition-2004

Observations● an instance of a class● state is maintained in fields● behaviour is implemented via methods● every object is uniquely identified

Page 3: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 3WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

UML Object Notation

object:class

accountNumber : String = “123”owner : String = “Jim Andrew”balance : double = 300.00

Representation● box with two

compartments● object identifier

– always underlined– anonymous objects– : → instanceOf

● attribute compartment– may omit some or all fields

:class

accountNumber : String = “123”owner : String = “Jim Andrew”balance : double = 300.00

Page 4: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 4WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

ClassesWhat are Classes● “the descriptor for a set of objects that share the same

attributes, operations, methods, relationships, and behaviour” UML Reference Manual, 2nd Edition-2004

● template for objects with similar features

Page 5: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 5WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

UML Class Notation

«entity»BankAccount

{author = Jim,status = tested }

-accountNumber : String -owner : String -balance : double

+create (number : String, owner : String) +deposit (amount : double) +getAccountNumber():String +getOwner():String

name compartment

attribute compartment

operation compartment

class name stereotype tagged value

visibilityclass scope operation

Page 6: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 6WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

UML Class Notation

«exercise»StudentToDo

+ reviewAndLearnMissingUMLClassNotations()

Page 7: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 7WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

How Much to Show?Scenarios● scenario 1: inter-class relationship

● scenario 2: behaviour of a class or classes

● scenario 3: entity classes representing data entities

Page 8: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 8WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

How Much to Show?Scenarios● scenario 1: inter-class relationship

– class boxes are probably enough with namecompartment only

● scenario 2: behaviour of a class or classes– class boxes with name & operations compartment

● scenario 3: entity classes representing data entities– ask the class?

Page 9: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 9WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Analysis ClassesWhat is an Analysis Class?● represents an important concept of the problem domain● maps on to real-world concepts● an attempt to capture the big picture

BankAccount accountNumber owner balance

deposit () getAccountNumber() getOwner()

SavingAccount

Q: what about savings account sub-class of BankAccount?

Page 10: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 10WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Analysis ClassesImportant Analysis Classes● «boundary»

– a class that mediates interaction between thesystem (or subsystem) and its environment

● «control»– a class that encapsulates use case specific behaviour

● «entity»– a class that is used to model persistence information

Page 11: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 11WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Analysis ClassesObservations● name reflects the intent of abstraction● focus clearly defined/understood● small & well defined set of responsibilities● high cohesion & low coupling

BankAccount accountNumber owner balance

deposit () withdraw() calculateInterest()

clientPromotion()

Q: responsibility, cohesion & coupling?Q: can we use analysis classes for implementation?

Page 12: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 12WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design ClassesWhat is a Design Class?● Integrates the problem domain & solution domain

– problem domain → refined analysis classes– solution domain → implementation specific classes

● are complete, ready to be implementedBankAccount

-accountNumber : String -owner : String -balance : double = 0

+BankAccount (owner:String, number:String) +deposit (m:double):void +withdraw (m:double):void - validateWithdraw():boolean +getOwner():String

1

2

3

Page 13: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 13WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design ClassesConcerns● responsibility

– the public methods imply a contract– code evolution should be considered

● inheritance– major concern of design classes (why?)

Page 14: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 14WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Inheritance Under ReviewConcerns● the strongest form of coupling between classes● “fragile base class” problem

– changes in the base class impact subclasses● inflexible to change

– fixed at runtime

Page 15: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 15WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Example: Ambitious JohnHow to promote john to be a manager?● john:Programmer vs. john:Manager

Page 16: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 16WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Example: Ambitious JohnAggregation to rescue employee has a job

Page 17: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 17WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Inheritance or InterfaceInheritance● interface & implementation● concerns with reuse

Interface● interface only● concerns with functionality contract

– “design by contract”● concerns with reuse (what?)

Page 18: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 18WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Further ConsiderationsStudent Self-Study● Templates● Nested classes● Multiple Inheritance

Page 19: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 19WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Association RelationshipWhat is an Association?● connection between two things (usually classes)● “for a link between two object, there must be an

association between the class of those objects”– link is an instance of an association– link can be represented by dependency relationship

Group User

admin:Group john:User

«instanceOf»«instanceOf» «instanceOf»

association

link

Page 20: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 20WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

ExampleDecipher this● a Company can have

exactly seven employees● a Person can be employed

by exactly one Company● a BankAccount can have exactly one owner● a Person may have zero to many BankAccounts● a BankAccount can have one or many operators● a Person may operate zero to many BankAccounts

Page 21: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 21WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Reflexive AssociationDefinition● A class has an association with itself

Page 22: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 22WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Reflexive AssociationClass Diagram

Object Diagram

Page 23: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 23WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

NavigationReview the following● navigation notation & rules● uni-direction & bi-direction associations

Page 24: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 24WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Types of AssociationComposition● strong whole/part (owner/slave) relationship between

classes

– can we have more than “1” parent?– what happens when the parent is destroyed?

Page 25: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 25WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Types of AssociationAggregation● weaker whole/part relationship between classes

– classes can exist on their own● observations

– a computer may be attached tozero or more printers

– many computers can use a printer– printer may exist even if there are no computers– aggregation is transitive

Page 26: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 26WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Association ClassesAnalysis classes

Design classes

Page 27: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 27WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Association ClassesObservations● association classes are purely analysis classes● convert association class into normal design class

– use the various relationships to clarify● association (aggregation, composition, dependency)

– additional constraints to refine the model● navigation, multiplicity

Page 28: These slides are NOT to be used as a replacement for ...a78khan/cs446/lectures/... · (part 2 Class diagram, Associations) CS 446/646 ECE452 May 6th, 2011 Material covered in this

2011-05-04CS446/646 ECE452 28WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Student StudyCompulsory Reading ● Sequence diagram● State diagram● Activity diagram