Chapter 9 Implementing Association Relationships

29
Chapter 9 - Implementing Association Relationships 1 Chapter 9 Implementing Association Relationships

description

Chapter 9 Implementing Association Relationships. Chapter 9 Topics. Review of Bradshaw Marina’s class diagram Implementing association relationships with one-to-one multiplicity between Java classes Navigating from one instance to another when there is a one-to-one relationship - PowerPoint PPT Presentation

Transcript of Chapter 9 Implementing Association Relationships

Page 1: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 1

Chapter 9

Implementing Association Relationships

Page 2: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 2

Chapter 9 Topics

• Review of Bradshaw Marina’s class diagram• Implementing association relationships with one-

to-one multiplicity between Java classes• Navigating from one instance to another when

there is a one-to-one relationship• Using the Vector class to create association

relationships with one-to-many multiplicity between Java classes

• Navigating one-to-many association relationships using methods of the Vector class

• Creating and using an association class with Java

Page 3: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 3

Reviewing Bradshaw Marina’s Class Diagram

• See Figure 9-1• Association relationships

– Depict how instances of the classes are associated or connected to one another

– Shown on the class diagram as lines connecting classes

– Indicate that the system requires information about these associations

Page 4: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 4

Reviewing Bradshaw Marina’s Class Diagram

• Association relationships– Can be shown as:

• Aggregation relationships– Strong association where one instance “contains”

the other» E.g., town contains shopping centers or stores

• Composition relationships– Strong association where one is composed of, or

“part of” another» E.g., walls that are part of a building

Page 5: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 5

Associating Customer with Boat: One-to-One Association

Relationship• Each direction of the association

relationship must be defined in Java– Mandatory / optional– Multiplicity

• To implement in Java:– Use a reference variable of one class as

an attribute of another class

Page 6: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 6

Page 7: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 7

Associating Customer with Boat: One-to-One Association

Relationship• Modifying the Customer Class

– To implement a one-to-one association with the Boat class:

• Add an attribute to Customer that holds a reference to a Boat instance (Figure 9-3)

– TesterOneA (Figure 9-4)• Tests one direction of association relationship

– A Customer owns a Boat

– Sequence diagram• Illustrates the interaction in TesterOneA

Page 8: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 8

Page 9: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 9

Associating Customer with Boat: One-to-One Association

Relationship• Modifying the Boat Class

– To implement a one-to-one association with the Customer class: (Figure 9-7)

• Add an attribute to Boat that holds a reference to a Customer instance

• Add accessor methods to establish the association relationship in both directions

– TesterOneB (Figure 9-8)• Tests both directions of association relationship

– A Customer owns a Boat– A Boat is owned by a Customer

Page 10: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 10

Adding Capability to the Boat Class

• Techniques to increase functionality– Make relationship mandatory rather than

optional (pp. 291)• E.g., when Boat is instantiated, it could

require that a Customer be specified– Thus, only Boats owned by Customers of the

business would be accepted into the system

– Modify the Boat’s tellAboutSelf method (pp. 292)• Return information about both Boat and

Customer

Page 11: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 11

Page 12: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 12

Page 13: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 13

Page 14: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 14

Page 15: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 15

Associating Dock and Slip: A One-to-Many Association Relationship

• Dock / Slip Relationships– Slip and Dock relationship

• A slip is attached to a dock• One-to-One relationship

– Similar to Customer and Boat relationship

– Dock and Slip relationship• A dock contains many slips• One-to-Many relationship

– Requires different approach

Page 16: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 16

Associating Dock and Slip: A One-to-Many Association Relationship

• Introducing the Dock Class– Contains:

• A Vector attribute– Implements the one-to-many relationship

• A method that returns the Vector attribute reference

Page 17: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 17

Page 18: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 18

Page 19: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 19

Associating Dock and Slip: A One-to-Many Association Relationship

• Associating the Slip Class With Dock (Figure 9-15)– Modified much like Boat class to

implement a mandatory one-to-one association relationship• Associates slip with dock

– Further modified to also set up a relationship with a Boat• Associates slip with boat

Page 20: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 20

Associating Dock and Slip: A One-to-Many Association Relationship

• Testing the ‘Dock Contains Slips’ Association Relationship – TesterThreeA (Figure 9-16)

• Tests both directions of association relationship

– One-to-Many» A Dock has multiple Slips

– One-to-one» A Slip resides in a Dock

Page 21: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 21

Page 22: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 22

Page 23: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 23

Associating Dock and Slip: A One-to-Many Association Relationship

• Adding the Boat and Customer Classes to the Example – To complete example (Figure 9-18):

• Modify Boat to associate with a Slip• Add Customer class

– TesterThreeB (Figure 9-19)• Provides a comprehensive test of associations

– Sequence diagram (Figure 9-20)• Illustrates the interaction in TesterThreeB

Page 24: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 24

Page 25: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 25

Page 26: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 26

Creating and Using an Association Class - Lease

• Association class– Lease is an association between a customer and a slip, but

with additional attributes to characterize the lease agreement

– To implement (Figure 9-23):• Lease modified to include Slip and Customer reference

attributes• Slip modified to include a Lease reference attribute

– Class diagram (Figure 9-22)• Shows relationships between classes

– TesterFour (Figure 9-26)• Provides a comprehensive test of overall Lease associations

– Sequence diagram• Illustrates the interaction in TesterFour

Page 27: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 27

Page 28: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 28

Page 29: Chapter 9 Implementing Association Relationships

Chapter 9 - Implementing Association Relationships 29