Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.

Similar presentations


Presentation on theme: "Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships."— Presentation transcript:

1 Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships

2 Object-Oriented Application Development Using VB.NET 2 Objectives In this chapter, you will: Identify association relationships on Bradshaw Marina’s class diagram Associate VB.NET classes in a one-to-one relationship Add functionality to the Boat class

3 Object-Oriented Application Development Using VB.NET 3 Objectives In this chapter, you will: Associate Dock and Slips in a one-to-many relationship Add the Boat and Customer classes to the Slip example Create and use an association class—Lease

4 Object-Oriented Application Development Using VB.NET 4 Identifying Association Relationships on Bradshaw Marina’s Class Diagram Association relationships –Show how instances of classes are associated, or connected, to each other –Indicate that the system requires information about these associations –Can be used to navigate from instance to instance following the association

5 Object-Oriented Application Development Using VB.NET 5 Identifying Association Relationships on Bradshaw Marina’s Class Diagram In a class diagram –An association relationship Appears as a line connecting classes –Multiplicity of the association Indicated by numbers written at both ends of the line

6 Object-Oriented Application Development Using VB.NET 6 Identifying Association Relationships on Bradshaw Marina’s Class Diagram Association relationships in the Bradshaw class diagram: –A customer owns a boat –A boat is owned by a customer –A boat is assigned to a slip –A slip contains a boat –A dock contains many slips –A slip is attached to a dock –A slip is leased to a customer (Lease is an association class) –A customer leases a slip (Lease is an association class)

7 Object-Oriented Application Development Using VB.NET 7 Identifying Association Relationships on Bradshaw Marina’s Class Diagram

8 Object-Oriented Application Development Using VB.NET 8 Identifying Association Relationships on Bradshaw Marina’s Class Diagram Lease class –An example of an association class Exists because of the relationship between a slip and a customer –In the class diagram Shown as a class connected by a dashed line to an association between Slip and Customer

9 Object-Oriented Application Development Using VB.NET 9 Associating VB.NET Classes in a One-to-One Relationship Association relationship between Customer and Boat –Has two “directions” A customer owns a boat A boat is owned by a customer –Each direction of the relationship Must be defined separately by the system developer Must be handled separately with VB.NET

10 Object-Oriented Application Development Using VB.NET 10 Associating VB.NET Classes in a One-to-One Relationship To implement an association relationship in VB.NET –Use a reference variable as an attribute of a class A reference variable points to an actual instance

11 Object-Oriented Application Development Using VB.NET 11 Associating VB.NET Classes in a One-to-One Relationship

12 Object-Oriented Application Development Using VB.NET 12 Associating VB.NET Classes in a One-to-One Relationship A Customer reference variable –Points to a Customer instance –If added as an attribute in the Boat class, each Boat instance can Point to a Customer instance Invoke the customer’s methods

13 Object-Oriented Application Development Using VB.NET 13 Associating VB.NET Classes in a One-to-One Relationship A Boat reference variable –Points to a Boat instance –If added as an attribute in the Customer class, each Customer instance can Point to a Boat instance Invoke the boat’s methods

14 Object-Oriented Application Development Using VB.NET 14 Modifying the Customer Class Modifying the Customer class implements one direction of the association relationship –From Customer to Boat theBoat attribute –A Boat reference variable –Added to the Customer class –Used to implement a one-to-one association relationship with the Boat class

15 Object-Oriented Application Development Using VB.NET 15 Modifying the Customer Class Accessor methods –SetBoat Accepts a Boat reference as a parameter Assigns the reference to the attribute –GetBoat Returns the Boat reference

16 Object-Oriented Application Development Using VB.NET 16 Modifying the Customer Class Constructor –Sets the Boat reference variable to Nothing using the SetBoat method –Once Customer instance is created Boat reference variable can be set to an actual Boat reference

17 Object-Oriented Application Development Using VB.NET 17 Modifying the Boat Class Modifying the Boat class implements the other direction of the association relationship –A boat is owned by a customer theCustomer attribute –A Customer reference variable –Added to the Boat class –Used to implement a one-to-one association relationship with the Customer class

18 Object-Oriented Application Development Using VB.NET 18 Modifying the Boat Class Accessor methods –GetCustomer Returns the Customer reference –SetCustomer Accepts a Customer reference as a parameter Assigns the reference to the attribute

19 Object-Oriented Application Development Using VB.NET 19 Adding Functionality to the Boat Class Functionality of classes that have association relationships can be increased AssignBoatToCustomer custom method –Added in Boat –Establishes the association between Boat and Customer in both directions in one step

20 Object-Oriented Application Development Using VB.NET 20 Adding Functionality to the Boat Class Mandatory association between Boat and Customer –Possible because Bradshaw Marina does not want to keep information about a boat if its owner is not a customer –Done by Adding a Customer reference parameter to the Boat constructor

21 Object-Oriented Application Development Using VB.NET 21 Adding Functionality to the Boat Class Modified Boat TellAboutSelf method –Returns information about Boat Customer –Possible because A boat must be associated with a customer

22 Object-Oriented Application Development Using VB.NET 22 Associating Docks and Slips: A One- to-Many Association Relationship Association relationship between Slip and Dock –Two directions of the relationship Association relationship between Slip and Dock –One-to-one »A slip is attached to a dock Association relationship between Dock and Slip –One-to-many »A dock contains many slips

23 Object-Oriented Application Development Using VB.NET 23 Associating Docks and Slips: A One- to-Many Association Relationship To implement –A one-to-one association relationship between Slip and Dock Use the technique used to implement the Customer and Boat associations –A one-to-many association relationship between Dock and Slip Requires that a dock instance have reference variables for more than one slip –Use an ArrayList in the Dock class to hold many Slip reference variables

24 Object-Oriented Application Development Using VB.NET 24 Associating Docks and Slips: A One- to-Many Association Relationship ArrayList class –Can be used to instantiate a container that can hold many reference variables –Has methods which can be used to Add Slip references Retrieve Slip references

25 Object-Oriented Application Development Using VB.NET 25 Introducing the Dock Class Dock class –Four previous attributes An ID number A location Two Boolean variables indicating –Whether the dock has electricity –Whether the dock has water –Fifth attribute slips –An ArrayList –Implements the one-to-many association relationship

26 Object-Oriented Application Development Using VB.NET 26 Introducing the Dock Class Dock class constructor –Sets values for the four attributes –Instantiates the new ArrayList –Assigns the new ArrayList to the slips attribute GetSlips –A getter method –Returns the slips reference variable

27 Object-Oriented Application Development Using VB.NET 27 Introducing the Dock Class AddSlip custom method –Used to add a slip to the dock –Invoked by the Slip AddSlipToDock method –Contains the slips.Add(aSlip) statement Used to add a Slip reference to the ArrayList referenced by the variable slips

28 Object-Oriented Application Development Using VB.NET 28 Associating the Slip Class with Dock Modifications to the Slip class to implement the mandatory one-to-one association relationship –theDock attribute A Dock reference variable –Accessor methods for the Dock reference attribute

29 Object-Oriented Application Development Using VB.NET 29 Associating the Slip Class with Dock Modifications to the Slip class (Continued) –Modified constructor Expects a Dock reference parameter –Result: When a slip is instantiated, it must be associated with a dock Invokes the slip’s AddSlipToDock method –AddSlipToDock method establishes the association in both directions by »Invoking the dock’s AddSlip method using the Me keyword

30 Object-Oriented Application Development Using VB.NET 30 Associating the Slip Class with Dock Modifications to the Slip class (Continued) –A TellAboutSelf method Returns information about –The slip –The slip’s dock

31 Object-Oriented Application Development Using VB.NET 31 Adding the Boat and Customer Classes to the Slip Example Association relationship between Slip and Boat –Modifications to the Slip class A Boat reference attribute Accessor methods –GetBoat –SetBoat –Modifications to the Boat class A Slip reference attribute Accessor methods AssignBoatToSlip method

32 Object-Oriented Application Development Using VB.NET 32 Adding the Boat and Customer Classes to the Slip Example To include the Customer class in this example –Boat class does not need to be modified It is already designed to associate with a customer Given a Customer reference –Can navigate to find The customer’s boat The boat’s slip The slip’s dock

33 Object-Oriented Application Development Using VB.NET 33 Adding the Boat and Customer Classes to the Slip Example Given a Dock reference –Can navigate to Each slip The slip’s boat The customer who owns the boat

34 Object-Oriented Application Development Using VB.NET 34 Creating and Using an Association Class – Lease Lease class –An association class A Lease is like an association between a customer and a slip, but with attributes for –Start date –End date –Amount of lease, and so on –Subclasses AnnualLease DailyLease

35 Object-Oriented Application Development Using VB.NET 35 Creating and Using an Association Class – Lease Modifications to the Lease superclass –A Slip reference attribute –A Customer reference attribute –Accessor methods AnnualLease class –A subclass of Lease –Inherits the association between Lease and Slip Lease and Customer

36 Object-Oriented Application Development Using VB.NET 36 Creating and Using an Association Class – Lease No modifications needed for the Boat class –Reason: Boat is not directly associated with Lease Modifications to the Customer class –A Lease reference attribute –Accessor methods

37 Object-Oriented Application Development Using VB.NET 37 Creating and Using an Association Class – Lease Modifications to the Slip class –A Lease reference attribute –Accessor methods –LeaseAnnualSlip method Creates an AnnualLease instance

38 Object-Oriented Application Development Using VB.NET 38 Summary Association relationships are shown on the class diagram as lines connecting classes Association relationships are implemented in two directions that must be considered separately Association relationships can be optional or mandatory One-to-one association relationships are implemented by including a reference variable as an attribute in one class that points to an instance of another class

39 Object-Oriented Application Development Using VB.NET 39 Summary One-to-many association relationships are implemented using an ArrayList that contains a collection of reference variables Association relationships can be directly navigated by writing one statement with multiple method calls that are executed from left to right An association class exists because of a relationship between two classes, such as Lease between Slip and Customer


Download ppt "Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships."

Similar presentations


Ads by Google