Class Diagrams Ferd van Odenhoven Fontys Hogeschool voor Techniek - - PowerPoint PPT Presentation

class diagrams
SMART_READER_LITE
LIVE PREVIEW

Class Diagrams Ferd van Odenhoven Fontys Hogeschool voor Techniek - - PowerPoint PPT Presentation

Objects and classes Relations between Classes Associations Class Diagrams Ferd van Odenhoven Fontys Hogeschool voor Techniek en Logistiek May 27, 2015 FvO/FHTBM Class Diagrams May 27, 2015 1/26 Objects and classes Diagrams Relations


slide-1
SLIDE 1

Objects and classes Relations between Classes Associations

Class Diagrams

Ferd van Odenhoven

Fontys Hogeschool voor Techniek en Logistiek

May 27, 2015

FvO/FHTBM Class Diagrams May 27, 2015 1/26

slide-2
SLIDE 2

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Objects and classes Basis concepts

Object

A selfcontained thing that exists in reality or in mind encapsulation Provides services: has responsibilities

Class

Description of objects with corresponding properties of a certain kind Determines attributes and operations Has objects as its real instances

FvO/FHTBM Class Diagrams May 27, 2015 2/26

slide-3
SLIDE 3

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Objects and classes

Account

name: String amount: double getAmount(): double

acc1:Account

name = "John" amount = 845.75

acc2:Account

name = "Peter" amount = 265.25

Class: Objects:

FvO/FHTBM Class Diagrams May 27, 2015 3/26

slide-4
SLIDE 4

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Modeling and Analysis

In the analysis phase the approach is sketchy, because the domain is still under investigation In the analysis phase we construct the Domain model! In this phase many changes occur in the class diagram: classes are being changed or removed and new classes are

  • added. Associations too are still not in their final state.

Hence we do not yet add methods/operations to the classes in these premature class diagrams! We restrict ourselves to find the properties of the domain classes.

FvO/FHTBM Class Diagrams May 27, 2015 4/26

slide-5
SLIDE 5

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Example: a company has an employee

1 employee Person Company Person Company employee: Person

These two diagrams are equivalent

FvO/FHTBM Class Diagrams May 27, 2015 5/26

slide-6
SLIDE 6

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Example: a company has employees

* employees Person Company Person Company employees: Person[*]

These two diagrams are equivalent

The multiplicity [*] has no relation to array!

FvO/FHTBM Class Diagrams May 27, 2015 6/26

slide-7
SLIDE 7

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Modeling and Analysis: domain model

A textual description can be a good starting point for a domain analysis. Every noun is a candidate for a new class. A property of a class can be added as a member or an association. If the property or attribute is itself a domain object, we prefer the association! Certainly in a domain model. CRC cards may be used to decide on the responsibilities of each class. Important use case scenario’s are tested on the class diagram using sequence diagrams. Adjustments to the classdiagram are still possible!

FvO/FHTBM Class Diagrams May 27, 2015 7/26

slide-8
SLIDE 8

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

CRC-cards

A CRC-card looks as follows : Class: Nr: Responsibilities: Collaborations:

Table : CRC-cards can help in dividing the responsibilities over the relevant classes.

FvO/FHTBM Class Diagrams May 27, 2015 8/26

slide-9
SLIDE 9

Objects and classes Relations between Classes Associations Diagrams Domain modeling CRC-cards

Example crc card for library

<<crc-card>> Library Responsibilies Make rentals and reservations for copies of books. Maintain members: add, remove, alter. Collaborations Copy, Book, Member.

In umlet you can find the template for this card in de mod1 palette.

FvO/FHTBM Class Diagrams May 27, 2015 9/26

slide-10
SLIDE 10

Objects and classes Relations between Classes Associations Associations Aggregation-Composition

Associations

Structural relationship between two classes Fact: A person works at a company. Arrowhead after text on association indicates reading direction and only that! More associations between two classes are allowed. Role names at the classes : ‘employee’ & ‘employer’.

Person Company employee employer works at

FvO/FHTBM Class Diagrams May 27, 2015 10/26

slide-11
SLIDE 11

Objects and classes Relations between Classes Associations Associations Aggregation-Composition

Associations and roles

Objects can have different roles (association end) Direction of an association:

Association: in general both objects know each other but: associations can be forced in one direction (must use

  • pen arrow head

→ )

FvO/FHTBM Class Diagrams May 27, 2015 11/26

slide-12
SLIDE 12

Objects and classes Relations between Classes Associations Associations Aggregation-Composition

Dependency

Model element describing which class is dependent of some

  • ther class.

Also showing more associations between two classes.

«uses» Passenger * 1 1 departure Flight Airport 1 destiny * arrivals * departures

Figure : Dependency relation: dashed arrow and stereotype with keyword ‘uses’.

FvO/FHTBM Class Diagrams May 27, 2015 12/26

slide-13
SLIDE 13

Objects and classes Relations between Classes Associations Associations Aggregation-Composition

Aggregation (only semantic meaning)

Aggregation: an association showing that classes are part of another class (white lozenge). Instances of the parts classes can also exist on their own. 2 1 Bicycle Frame Wheel

FvO/FHTBM Class Diagrams May 27, 2015 13/26

slide-14
SLIDE 14

Objects and classes Relations between Classes Associations Associations Aggregation-Composition

Composition (use only when necessary)

Composition: an association depicting that classes are part of another class (black lozenge). Instances of the parts-of class cannot exist on their own : ‘Life-Time Dependancy’ 1 Frame 2 Bicycle Wheel

FvO/FHTBM Class Diagrams May 27, 2015 14/26

slide-15
SLIDE 15

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Multiplicity

Shows the number of possible instances of an associated class. Directions of association and label are independent!

Person Person Person Person Car Car Car Car 1..* 0..1 transports * passengers transports 5 1 are transported by * * are transported by

It is recommended to add text lable to an association in a domain model.

FvO/FHTBM Class Diagrams May 27, 2015 15/26

slide-16
SLIDE 16

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

An example: the new Library

The renewed textual description. The public library Venlo has four locations: Belfeld, Tegelen, Blerick en Venlo. To borrow a book, one must be member of the library. Membership is valid for one year, starts at the month of subscription and one obtains a member card. Membership fees: no fee under 16 year, 10 e for ages 16-17 and 35 e for ages 18 and up. Students pay 30 e. With a subscription every member can make use of the central library, as well as the locations at Blerick, Belfeld,

  • Tegelen. Books, audio cassettes, magazines, video tapes, dvd

disks, cd-roms and daisy-rom can be borrowed. There are no fees for lending books or other media and there is no limit on the number of items per year. The maximum numbers of media items is 25 at any time.

FvO/FHTBM Class Diagrams May 27, 2015 16/26

slide-17
SLIDE 17

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

An example: the new Library

The lending period for media is three weeks. So called sprinters have a lendingperiod of two weeks. Extending a lending period can be done twice per item. There is no fee for making reservations. Extension of the lending period is not possible if a reservation has been made for the specific media item. If a book has been brought back after the lending period has expired, a fine of 20 cents per day is charged. Lending media or extending the lending period is not possible if the members’s debt at the library is larger than 25 euros.

FvO/FHTBM Class Diagrams May 27, 2015 17/26

slide-18
SLIDE 18

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Books and copies.

Fact: The author ’Joseph O’Connor’ has written a book with the title ’Star of the Sea’. The library possesses 5 copies of that book. We do need this trivial fact for our domain analysis.

Book

  • title: String
  • author: String

Copy

  • code: String
  • borrowed: boolean
  • isbn: String
  • publisher: String

1 *

Figure : What would be the direction of the association?

What to do if more data of the publisher are relevant?

FvO/FHTBM Class Diagrams May 27, 2015 18/26

slide-19
SLIDE 19

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Books and copies.

We see an example of a class diagram with two classes. For each class the attributes are given. A relation between the two classes is present. In this case it is an association. The association has a name and is to be interpreted from left to right. At the endings of the association the multiplicity has been

  • added. Read along the direction of the association: several

(*) copies of one (1) book.

FvO/FHTBM Class Diagrams May 27, 2015 19/26

slide-20
SLIDE 20

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Library.

The library owns many book copies. This is also a one (1) to many (*) association, but a different one. The library owns the books. Therefore it should be looked upon as a container object.

Library

  • name: String
  • location: String
  • address: Address
  • phone: String

Book

  • title: String
  • author: String

Copy

  • code: String
  • borrowed: boolean
  • isbn: String
  • publisher: String

* 1

  • f

1 *

  • wns

Figure : A library has copies of a book.

FvO/FHTBM Class Diagrams May 27, 2015 20/26

slide-21
SLIDE 21

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Library.

Library

  • name: String
  • location: String
  • address: Address
  • phone: String

Book

  • title: String
  • author: String

Copy

  • code: String
  • borrowed: boolean
  • isbn: String
  • publisher: String

* 1

  • f

1 *

  • wns

Figure : The library administrates copies, to every copy there belongs exactly one book.

All copies of a book have a reference to the book they belong to. In an object diagram we see e.g. three book copy objects of the same book, each of these objects has its own reference to the same book object.

FvO/FHTBM Class Diagrams May 27, 2015 21/26

slide-22
SLIDE 22

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Object diagram

book:Book copy1:Copy code=123 copy3:Copy code=125 :Library copy2:Copy code=127

Figure : Example of an object diagram for the domain model of the prevous slide.

FvO/FHTBM Class Diagrams May 27, 2015 22/26

slide-23
SLIDE 23

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Library, alternative modeling.

A library could also be seen as an institution offering a number of books of which copies can be lended. Each copy belongs to a book. As an example we could get the following model:

Library

  • name: String
  • location: String
  • address: Address
  • phone: String

Book

  • title: String
  • author: String

Copy

  • code: String
  • borrowed: boolean
  • isbn: String
  • publisher: String

1 *

  • ffers

1 *

  • f

The book class now manages the copies of the book. Better said: the library manages the book copies over the book class.

FvO/FHTBM Class Diagrams May 27, 2015 23/26

slide-24
SLIDE 24

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Association class

Association class: a class in connection with an association if the association has an attribute or if the association has an operation

FvO/FHTBM Class Diagrams May 27, 2015 24/26

slide-25
SLIDE 25

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

Classsical class model marriage

First attempt to import the association class in the class diagram.

Marriage

  • date: Date
  • town: String

Woman Man 0..1 1 0..1 1

There are of course more possibilities!

FvO/FHTBM Class Diagrams May 27, 2015 25/26

slide-26
SLIDE 26

Objects and classes Relations between Classes Associations Multiplicity and navigation Library. Association class

More general class model marriage

Man and woman are persons. The introduction of a Person class is obvious. The new class diagram, with the possibility of a marriage.

Marriage

  • date: Date
  • town: String

Person isMan: boolean isWoman: boolean 2 1 partners

FvO/FHTBM Class Diagrams May 27, 2015 26/26