database design – lecture 12 object oriented database design cont’d
TRANSCRIPT
Database Design – Lecture 12
Object Oriented Database Design cont’d
2
Lecture Objectives
Class Review Relationships Associations Aggregation/Composition Inheritance
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
4
Classes
Class Example
Employee
nameADDRESSdobtelephoneDEPENDENT
BENEFICIARY
Address
streetNumberstreetcityprovincepostalCodeEMPLOYEE
Denotes another abstract data Type (ADT); or another class
Dependent
nameDobEMPLOYEE
Beneficiary
nameRelationshipEMPLOYEE
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
6
Lecture Objectives
Classes Relationships Associations Aggregation/Composition Inheritance
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
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
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
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
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
12
Lecture Objectives
Classes Relationships Associations Aggregation/Composition Inheritance
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)
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
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
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
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
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
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
20
Lecture Objectives
Classes Relationships Associations Aggregation/Composition Inheritance
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
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
23
Aggregation/Composition
Example: An invoice has invoice items A car has tires A company has contacts
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
25
Aggregation vs Association
Which of these are composition? An invoice has invoice items A car has tires A company has contacts
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!
27
Lecture Objectives
Classes Relationships Assoications Aggregation/Composition Inheritance
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
29
Inheritance
Single Inheritance (~Hierarchical Structure)
A class has only one immediate (parent) superclass above it
30
Inheritance
Multiple Inheritance (~Network Structure)
A class has more than one immediate (parent) superclass above it
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
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
33
Inheritance
Generalization
+calculateBonus()
AdminStaff
+calculateBonus()
-qualifications
CreativeStaff
+calculateBonus()
-name-address
StaffMember
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
35
Inheritance
Defining Inheritance
Specialization
+calculateBonus()
AdminStaff
+calculateBonus()
-qualifications
CreativeStaff
+calculateBonus()
-name-address
Staff