jan 2003ron mcfadyen 91.39131 generalization (ch 26) a generalization is a relationship between a...

24
Jan 2003 Ron McFadyen 91.3913 1 zation (Ch 26) lization is a relationship between a general thing ss or parent class) and a more specific kind of thi class or child class) OfStaff is a specialized kind of LibraryMember LibraryMember MemberOfStaff

Post on 18-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 1

Generalization (Ch 26)

a generalization is a relationship between a general thing (the superclass or parent class) and a more specific kind of thing (the subclass or child class)example:a MemberOfStaff is a specialized kind of LibraryMember

LibraryMember

MemberOfStaff

Page 2: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 2

Generalization

examplecontinued:

•everything a LibraryMember can do, a MemberOfStaff can do•a LibraryMember can borrow a book, so can a MemberOfStaff•there are some things that a MemberOfStaff can do that a LibraryMember cannot (the specialization aspect)•the MemberOfStaff has all the attributes the LibraryMember has, and possibly more (again, the specialization aspect)

LibraryMember

MemberOfStaff

Page 3: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 3

a Generalization example:

•a SavingsAccount is a specialized kind of BankAccount•a ChequingAccount is a specialized kind of BankAccount•an Asset may be a BankAccount, RealEstate, or a Security

Asset

RealEstate BankAccount Security

SavingsAccount ChequingAccount

Page 4: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 4

Text:

All members of a conceptual subclass set are members of their superclass set

100% of the conceptual superclass’s definition should be applicable to the subclass.

The subclass must conform to 100% of the superclass’s:•attributes•associations

Page 5: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 5

Figure 26.7

PaymentAmount: money

Cash Payment Credit Payment Cheque Payment

SalePays-for1 1

Every payment, regardless of whether it is cash, credit, or cheque, has an Amount and it is associated with a Sale

CreditCard Cheque

1

1

1

*

Page 6: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 6

Motivation for partitioning a conceptual class into subclasses:

•the subclass has additional attributes of interest

•the subclass has additional associations of interest

•the subclass is operated on, handled, reacted to, or manipulated differently than the superclass or other subclasses

•the subclass concept represents an animate thing that behaves differently than the superclass or other subclasses

Examples

•a chequing account will accrue interest differently from a savings account

•a loan account will have different associations from a savings account

Page 7: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 7

Figure 26.12

The name Payment is italicized - meaning it is an abstract class

An abstract class is a class that will never be instantiated; only its subclasses can exist

If “Payment” was not in italics then a Payment could exist that is not a Cash, Credit, or Check payment

Page 8: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 8

Figure 26.7

PaymentAmount: money

Cash Payment Credit Payment Cheque Payment

SalePays-for1 1

Credit and Cheque participate in other associations

CreditCard Cheque

1

1

1

*

Page 9: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 9

Watch for objects that are performing roles, or moving from one state to another.

ROT: Do not model with subclasses. Model with states.

Text:

Consider payments that begin as unauthorized and may eventually become authorized.

Do not model as: Payment

Unauthorized Payment

Authorized Payment

Page 10: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 10

Text:

Instead of Payment

Unauthorized Payment

Authorized Payment

Use:

PaymentState

Unauthorized State Authorized State

Payment Is-in

* 1

Figure 26.13

Page 11: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 11

PaymentState

Unauthorized State Authorized State

Payment Is-in* 1

authorize()

cancel()

cancel()

getStatus()

cancel()

A payment that is not authorized has an authorize operation, but authorize cannot be executed by a payment that has already been authorized.

Cancel has two different implementations (polymorphism). Every payment has getStatus and

cancel operations

Page 12: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 12

dog cat

Constraints

Shown inside curly braces ({constraint})

•incomplete

•complete

•disjoint

•overlapping mammal

{disjoint, incomplete}

Page 13: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 13

Ch 27 - Refining the Domain Model

27.1 Association Classes

27.2 Aggregation and Composition

27.4 Association Role Names

27.5 Roles as Concepts vs Roles in Associations

27.8 Reflexive Associations

27.10 Packages

Page 14: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 14

Aggregation and Composition

both are associations used to denote that an object from one class is part of an object of another class

HonoursProgramme Course

An example of Aggregation: a course is part of an honours programme. The same module could be part of several honours courses

“Software Engineering with Objects and Components” is part of both the “Software Engineering” and the “Computer Science” honours programmes

**

Page 15: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 15

Aggregation and Composition

Board Square

Composition is similar to, but stronger than aggregation. If you specify composition, then you are saying that one object owns its parts.

A Board is made up of several Squares. A Square will belong to just one Board. If a Board is deleted, then its Squares are deleted too.

What is the multiplicity at the composition end of the association?

Page 16: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 16

Aggregation and Composition

Invoice InvoiceLine

Consider Invoices and their Invoice Lines

Question: Is the association aggregation or composition?

?

?

*

Page 17: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 17

Aggregation and Composition

Sale SalesLineItem

Consider Sales and their SalesLineItems

Is the association an aggregation or a composition?

?

?

*

Consider the Product Catalogue and its Product Specifications.

Is the association an aggregation or a composition?

Page 18: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 18

Aggregation and Composition

Figure 27.6

HandFinger

Palm

Thumb

0..7

0..1

0..1

Several associations sharing the same aggregation/composition can be drawn with converging lines

Page 19: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 19

Aggregation and Composition

Suppose a document comprises a TOC, a preface, a number of sections, and possibly an index.

Use composition or aggregation?

Page 20: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 20

Association Class

Figure: 27.3:

Business rule: a store has a different merchant ID for each service

Association Class: a modeling element that is both an association and a class. It has attributes, operations, multiplicities, etc. It can participate in other relationships.

Page 21: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 21

Top of Figure 27.4 (ignore rest of it)

Many to many associations

•likely candidate for an Association Class

Limitation : each company object can be associated to a person object only once. If we wanted to provide for a person to be employed by the same company more than once, we would need to promote Employment to be a regular class. How do we model this?

Page 22: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 22

Reflexive Association

An association involving the same class more than once.

marries

Person1

1

comprises

Part*

*

Page 23: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 23

Figure 27.11 Contrasting two models

Can be very accurate: one person plays more than one role

More difficult to implement: may need to mutate an object from one class to another

Page 24: Jan 2003Ron McFadyen 91.39131 Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a

Jan 2003 Ron McFadyen 91.3913 24

Packages

Packages are used to organize model elements.

Figures

27.21 shows the POS Domain package comprising the Payments, Product, etc. packages

27.22 the core package

27.23 the payments package

name