database design – lecture 12 object oriented database design cont’d

35
Database Design – Lecture 12 Object Oriented Database Design cont’d

Upload: lesley-farmer

Post on 18-Jan-2016

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Design – Lecture 12 Object Oriented Database Design cont’d

Database Design – Lecture 12

Object Oriented Database Design cont’d

Page 2: Database Design – Lecture 12 Object Oriented Database Design cont’d

2

Lecture Objectives

Class Review Relationships Associations Aggregation/Composition Inheritance

Page 3: Database Design – Lecture 12 Object Oriented Database Design cont’d

3

Classes

ClassName

attributes

operations()

Structure:

• Attribute names start with a small capital. If an attribute name is made up of multiple words, there is no space between the words and each subsequent word begins with a capital letter• Operation names start with a small capital. If an operation name is made up of multiple words, there is no space between the words and each subsequent word begins with a capital letter

Page 4: Database Design – Lecture 12 Object Oriented Database Design cont’d

4

Classes

Class Example

Employee

nameADDRESSdobtelephoneDEPENDENT

BENEFICIARY

Address

streetNumberstreetcityprovincepostalCodeEMPLOYEE

Denotes another abstract data Type (ADT); or another class

Dependent

nameDobEMPLOYEE

Beneficiary

nameRelationshipEMPLOYEE

Page 5: Database Design – Lecture 12 Object Oriented Database Design cont’d

5

Classes

Class Example (different notation)

Employee

nameaddressdobtelephonedependents

beneficiary

Address

streetNumberstreetcityprovincepostalCode

Denotes another abstract data Type (ADT); or another class

Dependent

namedob

Beneficiary

namerelationship

employee

employee

employee

Page 6: Database Design – Lecture 12 Object Oriented Database Design cont’d

6

Lecture Objectives

Classes Relationships Associations Aggregation/Composition Inheritance

Page 7: Database Design – Lecture 12 Object Oriented Database Design cont’d

7

Relationships 1:1 I.e. An employee has 1 address and an address belongs to 1 employee

Address

streetNumberstreetcityprovincepostalCodeemployee 1

Employee

nameaddress 1dobtelephonedependent

beneficiaryDenotes amandatoryparticipation

Page 8: Database Design – Lecture 12 Object Oriented Database Design cont’d

8

Relationships 1:M I.e. An employee has 0 or more dependents and a dependent belongs to 1 employee

Employee

nameaddress 1dobtelephonedependent M

beneficiary (no line) denotes anoptionalparticipation

Dependent

namedobemployee 1

Page 9: Database Design – Lecture 12 Object Oriented Database Design cont’d

9

Relationships M:N (option 1) I.e. An employee has 1 or more beneficiaries and a beneficiary belongs to 1 or more

employees

employee M

Beneficiary

namepercentrelationship

Employee

nameaddress 1dobtelephonedependent M

beneficiary M

Page 10: Database Design – Lecture 12 Object Oriented Database Design cont’d

10

Relationships M:N (option 2) I.e. An employee has 1 or more beneficiaries and a beneficiary belongs to 1 or more employees Create an intersection class with associated attributes

employeeBeneficiary M

Beneficiary

name

Employee

nameaddress 1dobtelephonedependent M

employeeBeneficiary M

employee 1

EmployeeBeneficiary

percent relationship

beneficiary 1

Page 11: Database Design – Lecture 12 Object Oriented Database Design cont’d

11

Relationships – two notations

Employee

nameaddress 1dobtelephonedependent M

beneficiary M

Dependent

namedobemployee 1

employee M

Beneficiary

namepercentrelationship

Note how we can show M:N relationship in OODM

Page 12: Database Design – Lecture 12 Object Oriented Database Design cont’d

12

Lecture Objectives

Classes Relationships Associations Aggregation/Composition Inheritance

Page 13: Database Design – Lecture 12 Object Oriented Database Design cont’d

13

Associations An association between two classes indicates

the possibility that links will exist between the objects of the classes

The links provide the ability for message passing to happen

Links can be uni-directional or bi-directional associations as well as more complex type links such as aggregation/composition – need to clearly understand the navigability of the association

Links also show multiplicity (1:1; 1:M; M:N)

Page 14: Database Design – Lecture 12 Object Oriented Database Design cont’d

14

Associations

1:1 Can be bi-directional or uni-directional

which shows the direction a message can be sent

Bi-directional – message goes in both directions

Address

streetNumberstreetcityprovincepostalCodeemployee 1

Employee

namedob

address 111 Lives at

Bi-directional; message goes in both directions

Page 15: Database Design – Lecture 12 Object Oriented Database Design cont’d

15

Associations

1:1 Uni-directional – message goes in direction

of arrow only Note there is no reference to the Employee

class in the Address classAddress

streetNumberstreetcityprovincepostalCode

Employee

namedob

address 11Lives at

Uni-directional; message goes in one direction only

**Note there is no reference to employee abstract data type (ADT) in the Address class

Page 16: Database Design – Lecture 12 Object Oriented Database Design cont’d

16

Associations

1:M Can be bi-directional or uni-directional What would change if we made it a uni-

directional association?Employee

nameaddress 1dobtelephonedependent M

beneficiary

Dependent

namedobemployee 1

1 0..nCan have

Page 17: Database Design – Lecture 12 Object Oriented Database Design cont’d

17

Associations

1:M Can be bi-directional or uni-directional What would change if we made it a uni-

directional association? – see belowEmployee

nameaddress 1dobtelephonedependent M

beneficiary

Dependent

namedobemployee 1

0..nCan have **Note there would be no reference to employee abstract data type (ADT) in the Dependent class

Page 18: Database Design – Lecture 12 Object Oriented Database Design cont’d

18

Associations

M:N Can be bi-directional or uni-directional What would change if we made it a bi-

directional association?Employee

nameaddress 1dobtelephonedependent M

beneficiary M

Beneficiary

namerelationship

1..nCan have

Page 19: Database Design – Lecture 12 Object Oriented Database Design cont’d

19

Associations

M:N Can be bi-directional or uni-directional What would change if we made it a bi-

directional association? – see belowEmployee

nameaddress 1dobtelephonedependent M

beneficiary M

Beneficiary

namerelationship

employee M

1..n 1..nCan have**Note a reference to employee abstract data class would be added

Page 20: Database Design – Lecture 12 Object Oriented Database Design cont’d

20

Lecture Objectives

Classes Relationships Associations Aggregation/Composition Inheritance

Page 21: Database Design – Lecture 12 Object Oriented Database Design cont’d

21

Aggregation/Composition A special kind of association. A whole (assembly) –part (components)

relationship An instance of one class may consist of, or

include instances of another class. The aggregate class assumes a special role for

delegation of responsibility and leadership The number of ‘parts’ are shown by multiplicity Aggregations are usually 1-to-many (aggregate

to components) but not always

Page 22: Database Design – Lecture 12 Object Oriented Database Design cont’d

22

Aggregation/Composition A “has a” or “consists of” relationship A Book contains Chapters

Book Chapter

contains

Aggregate or whole Components or part

Diamond symbol denotes an aggregation

Page 23: Database Design – Lecture 12 Object Oriented Database Design cont’d

23

Aggregation/Composition

Example: An invoice has invoice items A car has tires A company has contacts

Page 24: Database Design – Lecture 12 Object Oriented Database Design cont’d

24

Aggregation/Composition Composition is a special kind of aggregation. The component parts depend on the

aggregate for existence—delete the aggregate and the components disappear

Book Chapter

contains

Aggregate or whole Component or part

Filled in diamond symbol denotes a composition

Page 25: Database Design – Lecture 12 Object Oriented Database Design cont’d

25

Aggregation vs Association

Which of these are composition? An invoice has invoice items A car has tires A company has contacts

Page 26: Database Design – Lecture 12 Object Oriented Database Design cont’d

26

Aggregation vs Association

Show compositions in OODM only – aggregations can be shown as associations only

If you are not clear on the relationship between two classes, assume it is only an association – do not assume it has to be aggregation, composition or inheritance!

Page 27: Database Design – Lecture 12 Object Oriented Database Design cont’d

27

Lecture Objectives

Classes Relationships Assoications Aggregation/Composition Inheritance

Page 28: Database Design – Lecture 12 Object Oriented Database Design cont’d

28

Inheritance

Classes are organized into a hierarchy Hierarchy equates to inheritance Inheritance allows an object to inherit

the data structure and behavior of the classes above it

In OO systems, all objects are derived from the super class Object or the Root class

Page 29: Database Design – Lecture 12 Object Oriented Database Design cont’d

29

Inheritance

Single Inheritance (~Hierarchical Structure)

A class has only one immediate (parent) superclass above it

Page 30: Database Design – Lecture 12 Object Oriented Database Design cont’d

30

Inheritance

Multiple Inheritance (~Network Structure)

A class has more than one immediate (parent) superclass above it

Page 31: Database Design – Lecture 12 Object Oriented Database Design cont’d

31

Inheritance

Defining Inheritance Two approaches:

Generalization Look for common attributes and operations in a

set of classes and create a new superclass and place the common attributes and operations into the superclass

Specialization Create two or more subclasses to associate

attributes and methods to

Page 32: Database Design – Lecture 12 Object Oriented Database Design cont’d

32

Inheritance

Defining Inheritance Generalization

Look for common attributes and operations in a set of classes and create a new superclass and place the common attributes and operations into the superclass

Two classes that share common attributes and operations create a new class called StaffMember and move common attributes and operations to new superclass

+calculateBonus()

-name-address-qualification

CreativeStaff

+calculateBonus()

-name-address

AdminStaff

Page 33: Database Design – Lecture 12 Object Oriented Database Design cont’d

33

Inheritance

Generalization

+calculateBonus()

AdminStaff

+calculateBonus()

-qualifications

CreativeStaff

+calculateBonus()

-name-address

StaffMember

Page 34: Database Design – Lecture 12 Object Oriented Database Design cont’d

34

Inheritance

Defining Inheritance

Specialization

One class however, it may be determined that there are different types of staff members resulting in the creation of some sub classes

+calculateBonus()

-name-address

Staff

Page 35: Database Design – Lecture 12 Object Oriented Database Design cont’d

35

Inheritance

Defining Inheritance

Specialization

+calculateBonus()

AdminStaff

+calculateBonus()

-qualifications

CreativeStaff

+calculateBonus()

-name-address

Staff