Sei sulla pagina 1di 39

OO Design: Class Diagrams

Class Models
Essentials of class diagrams More sophisticated associations Reading: Chapter 4 of M. Page-Jones(2000) Fundamentals of Object-Oriented Design in UML. Chapters 5 and 6 of P. Stevens (2000) Using UML: Software engineering with objects and components.

Overview of UML.

Pablo Romero. Informatics Department

Class relations diagram


To identify real-world associations between

classes
To check understanding of the domain To check coupling in the system

Human maintainer recognises dependencies Modules can be re-used together

Overview of UML.

Pablo Romero. Informatics Department

OO design

Noun identication technique

Scenario walk-through

Specify associations Class Diagrams

Problem statement

Rough classes

CRC Cards

Overview of UML.

Pablo Romero. Informatics Department

From CRC Cards to Class Diagrams


Cards are classes Responsibilities are operations (Some) attributes are associations Associations have to be worked out

Multiplicity Ownership Navigability Attributes and operations


Pablo Romero. Informatics Department

Overview of UML.

The initial version


Books and Journals. The library contains books and journals. It might have several copies of a given book. Some of the books are for short term loans only. All other books might be borrowed by any library member for three weeks. Only members of staff may borrow journals. Members of the library can normally borrow up to six items at a time, but members of staff may borrow up to 12. Borrowing. It is essential that the system keeps track of when books and journals are borrowed and returned, enforcing the rules described above.

Overview of UML.

Pablo Romero. Informatics Department

Records borrowed copy

Overview of UML.

Pablo Romero. Informatics Department

Simple Class Diagram

Overview of UML.

Pablo Romero. Informatics Department

Initial class relations diagram


Classes Associations

Overview of UML.

Pablo Romero. Informatics Department

Revised class relations diagram

Generalisation relationship

10

Overview of UML.

Pablo Romero. Informatics Department

Including the Library class


Library contains

is a member of

11

Overview of UML.

Pablo Romero. Informatics Department

Multiplicities
To specify amounts in associations Notation

Exact number Range of numbers (1..9) Arbitrary number (*) A combination of these separated by comma (3, 12..15, 901..*)

12

Overview of UML.

Pablo Romero. Informatics Department

Multiplicities

1..*

13

Overview of UML.

Pablo Romero. Informatics Department

Library system with multiplicities


Library contains

is a member of

14

Overview of UML.

Pablo Romero. Informatics Department

Library system with multiplicities


Library 1 1 is a member of 0..* contains 1..*

0..6

1 15 Overview of UML.

0..12 Pablo Romero. Informatics Department

16

Overview of UML.

Pablo Romero. Informatics Department

Cinema booking with multiplicities


Cinema booking manages Showing

screened in

Row

contains

Screen

contains

Seat 17 Overview of UML.

reserves

Customer Pablo Romero. Informatics Department

Cinema booking with multiplicities


Cinema booking 1 manages 1..* 1..* screened in 1 Row 1..* 1 contains 1..* Seat 1..* 18 Overview of UML. reserves 1 Pablo Romero. Informatics Department Customer contains 1 Screen Showing

Attributes and operations

19

Overview of UML.

Pablo Romero. Informatics Department

Generalisation

inheritance

20

Overview of UML.

Pablo Romero. Informatics Department

Class diagram for library


Library
1 validateMember: Boolean validateBook: Boolean borrowBook: Boolean 1

Book
contains
1..* availableCopy: Boolean ndAvailable: Copy borrowCopy: Boolean 1

is a member of
0..*

is a copy of
1..*

LibraryMember
1 borrowingLimit: integer

Copy
borrows
0..6 getAvailStatus: Boolean setAvailStatus: void

21

Overview of UML.

Pablo Romero. Informatics Department

Aggregation and composition


Ownership relationships Aggregation weak ownership Composition strong ownership Diamond shape on the side of the owner

22

Overview of UML.

Pablo Romero. Informatics Department

Aggregation

Programme

Course

23

Overview of UML.

Pablo Romero. Informatics Department

Composition

24

Overview of UML.

Pablo Romero. Informatics Department

Composition and aggregation


Library
1 validateMember: Boolean validateBook: Boolean borrowBook: Boolean 1

Book
contains
1..* availableCopy: Boolean ndAvailable: Copy borrowCopy: Boolean 1

is a member of
0..*

is a copy of
1..*

LibraryMember
1 borrowingLimit: integer

Copy
borrows
0..6 getAvailStatus: Boolean setAvailStatus: void

25

Overview of UML.

Pablo Romero. Informatics Department

Composition and aggregation


Library
1 validateMember: Boolean validateBook: Boolean borrowBook: Boolean 1 0..*

Book
contains
1..* availableCopy: Boolean ndAvailable: Copy borrowCopy: Boolean

is a member of

1 is a copy of 1..*

LibraryMember
1 borrows borrowingLimit: integer 1..6

Copy
getAvailStatus: Boolean setAvailStatus: void

26

Overview of UML.

Pablo Romero. Informatics Department

Roles

27

Overview of UML.

Pablo Romero. Informatics Department

Navigability

Module knows about student


28 Overview of UML. Pablo Romero. Informatics Department

Navigability
Library
1 validateMember: Boolean validateBook: Boolean borrowBook: Boolean 1 0..*

Book
contains
1..* availableCopy: Boolean ndAvailable: Copy borrowCopy: Boolean

is a member of

1 is a copy of 1..*

LibraryMember
1 borrows borrowingLimit: integer 1..6

Copy
getAvailStatus: Boolean setAvailStatus: void

29

Overview of UML.

Pablo Romero. Informatics Department

Navigability
Library
1 validateMember: Boolean validateBook: Boolean borrowBook: Boolean 1 0..*

Book
contains
1..* availableCopy: Boolean borrowCopy:Copy returnCopy: Boolean

is a member of

1 is a copy of 1..*

LibraryMember
1 borrows borrowingLimit: integer 1..6

Copy
getAvailStatus: Boolean setAvailStatus: void

30

Overview of UML.

Pablo Romero. Informatics Department

Cinema booking with ownership and navigability relationships


Cinema booking 1 manages 1..* 1..* screened in 1 Row 1..* 1 contains 1..* Seat 1..* 31 Overview of UML. reserves 1 Pablo Romero. Informatics Department Customer contains 1 Screen Showing

Cinema booking with ownership and navigability relationships


Cinema booking 1 manages 1..* 1..* screened in 1 Row 1..* 1 contains 1..* Seat 1..* 32 Overview of UML. reserves 1 Pablo Romero. Informatics Department Customer contains 1 Screen Showing

Qualified associations

33

Overview of UML.

Pablo Romero. Informatics Department

Constraints

34

Overview of UML.

Pablo Romero. Informatics Department

Association classes

35

Overview of UML.

Pablo Romero. Informatics Department

Designing class interfaces


Replay the scenarios in terms of method calls,

parameters and return values. Note down the resulting signatures. Create outline classes with detailed field and method descriptions. Careful design is a key to successful implementation.

36

Overview of UML.

Pablo Romero. Informatics Department

Modified Book class


Book
title: String author:String year: int numberOfCopies : int availableCopy: Boolean borrowCopy:Copy returnCopy: Boolean

37

Overview of UML.

Pablo Romero. Informatics Department

/** * This class implements a container which * represents a book of the library * * @author Pablo Romero * @version 2006.08.10 */ public class Book { // The title, its author, year of publication, // number of copies and the array of copies private String title; private String author; private int year; private int numberOfCopies; private ArrayList<Copy> Copies; /** * Create a new book * @param t the book's title * @param a the book's author * @param y the year of publication * @param n number of copies **/ public Book(String t, String a, int y, int n) { } } 38 Overview of UML.

/** * Veries whether there are available copies * of the book * @return true if there are available copies, * false otherwise */ public boolean availableCopy () { } /** * Finds an available copy of the book for a * member to borrow * @return an available copy of the book */ public Copy borrowCopy () { } /** * Returns a copy of the book * @return true if the copy is a valid copy * of the book, false otherwise */ public boolean returnCopy (Copy returnedCopy) { } Pablo Romero. Informatics Department

Summary
UML specifies restrictions on class

relationships through
multiplicities, generalisation, aggregation, composition, roles, navigability, qualified associations and constraints

39 Overview of UML. Pablo Romero. Informatics Department

Potrebbero piacerti anche