Title: Chapter 9 Implementing Association Relationships Dr. James Jiang
1Chapter 9Implementing Association
RelationshipsDr. James Jiang
2Chapter 9 Topics
- Implementing association relationships with
one-to-one multiplicity between Java classes - Navigating from one instance to another
- Creating association relationships with
one-to-many multiplicity between Java classes - Navigating one-to-many association relationships
- Creating and using an association class with Java
3Reviewing Bradshaw Marinas Class Diagram
- 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
4(No Transcript)
5Associating 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
6(No Transcript)
7Associating 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
8Modified Customer class definition
9TesterOneA.java associate Customer with Boat (I)
10TesterOneA.java associate Customer with Boat
(II)
11Associating Customer with Boat One-to-One
Association Relationship
- Modifying the Boat Class
- To implement a one-to-one association with the
Customer class - Add an attribute to Boat that holds a reference
to a Customer instance - Add a method to establish the association
relationship in both directions
12Modified Boat class definition (I)
13Modified Boat class definition (II)
14TesterOneB.java associate Boat with Customer
15Adding Capability to the Boat Class
- Techniques to increase functionality
- Make relationship mandatory
- 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 Boats tellAboutSelf method
- Return information about both Boat and Customer
16Mandatory
17(No Transcript)
18(No Transcript)
19Associating 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
- Dock and Slip relationship
- A dock contains many slips
- One-to-Many relationship
- Requires different approach
20Associating 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
21(No Transcript)
22(No Transcript)
23Associating Dock and Slip A One-to-Many
Association Relationship
- Associating the Slip Class With Dock
- 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
24The Slip class definition
25Associating Dock and Slip A One-to-Many
Association Relationship
- Testing the Dock Contains Slips Association
Relationship - TesterThreeA
- Tests both directions of association relationship
- One-to-Many
- A Dock has multiple Slips
- One-to-one
- A Slip resides in a Dock
26(No Transcript)
27(No Transcript)
28Associating Dock and Slip A One-to-Many
Association Relationship
- Adding the Boat and Customer Classes to the
Example - To complete example
- Modify Boat to associate with a Slip
- Add Customer class
29Boat class definition associates with Slip (I)
30Boat class definition associates with Slip (II)
31Boat class definition associates with Slip (III)
32TesterThreeB.java Dock has Slips with Boat and
Customer (I)
33TesterThreeB.java Dock has Slips with Boat and
Customer (II)
34(No Transcript)
35Creating 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
- Lease modified to include Slip and Customer
reference attributes - Slip modified to include a Lease reference
attribute
36(No Transcript)
37Modified Lease class definition
38Modified Slip class definition (I)
39Modified Slip class definition (II)
40TesterFour.java (I)
41TesterFour.java (II)
42(No Transcript)