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 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

3 Object-Oriented Application Development Using VB.NET 3 Identifying Association Relationships on Bradshaw Marina’s Class Diagram Association relationships –Show how instances of classes are associated, or connected, to each other. –Can be used to navigate from instance to instance following the association 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

4 Object-Oriented Application Development Using VB.NET 4 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)

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

6 Object-Oriented Application Development Using VB.NET 6 Identifying Association Relationships on Bradshaw Marina’s Class Diagram Association relationship can be shown also as: –Aggregation relationship: One Instance contains another instance. For example, A town contains Shopping centers –Composition relationship: One Instance is composed of, or part of another instance. For example, Walls is a part of building. Lease class ( association class) Exists because of the relationship between a slip and a customer Shown as a class connected by a dashed line to an association between Slip and Customer

7 Object-Oriented Application Development Using VB.NET 7 Associating VB.NET Classes in a One-to-One Relationship Association relationship between Customer and Boat has two “directions” A customer owns a boat (Optional relationship) A boat is owned by a customer (Mandatory relationship) Each direction of the relationship must be defined separately by the system developer.

8 Object-Oriented Application Development Using VB.NET 8 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. It is a reference variable points to an actual instance. –Though, you can invoke the methods of that instance using that attribute.

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

10 Object-Oriented Application Development Using VB.NET 10 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

11 Object-Oriented Application Development Using VB.NET 11 Associating VB.NET Classes in a One-to-One Relationship In the same way, 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

12 Object-Oriented Application Development Using VB.NET 12 Modifying the Customer Class Adding theBoat attribute –A Boat reference variable –Added to the Customer class –Used to implement a one-to-one association relationship with the Boat class –Adding an attribute:- Private theBoat As Boat

13 Object-Oriented Application Development Using VB.NET 13 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 Accessor methods –SetBoat Public sub setBoat( aBoat as Boat) theBoat = aBoat End Sub –GetBoat Public Function getBoat( ) as Boat Return theBoat End Sub

14 Object-Oriented Application Development Using VB.NET 14 Modifying the Boat Class Adding theCustomer attribute –A Customer reference variable –Added to the Boat class –Used to implement a one-to-one association relationship with the Customer class –Adding an attribute: - Private theCustomer as Customer

15 Object-Oriented Application Development Using VB.NET 15 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 –Note: More powerful of association is to invoke a series of methods. For example : - Cus1.getBoat( ).getLength( )

16 Object-Oriented Application Development Using VB.NET 16 Adding Functionality to the Boat Class Functionality of classes that have association relationships can be increased AssignBoatToCustomer custom method –Added in Boat Class. –Establishes the association between Boat and Customer in both directions in one step: - Public Sub AssignBoatToCustomer ( ByVal C1 As Customer) setCustomer ( C1) C1.setBoat(Me) End Sub

17 Object-Oriented Application Development Using VB.NET 17 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

18 Object-Oriented Application Development Using VB.NET 18 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 Public Function TellAboutSelf( ) As String Return “Attributes of Boat” & theCustomer.TellAboutSelf( ) End Function

19 Object-Oriented Application Development Using VB.NET 19 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

20 Object-Oriented Application Development Using VB.NET 20 Associating Docks and Slips: A One-to-Many Association Relationship To implement one-to-one association relationship between Slip and Dock –Use the same 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

21 Object-Oriented Application Development Using VB.NET 21 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

22 Object-Oriented Application Development Using VB.NET 22 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 –Ex: Private slips as ArrayList

23 Object-Oriented Application Development Using VB.NET 23 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 Ex: slips = New ArrayList( ) GetSlips Public Function GetSlips( ) As ArrayList Return slips End Function

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

25 Object-Oriented Application Development Using VB.NET 25 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 Private theDock As DocK –Accessor methods for the Dock reference attribute GetDock SetDock

26 Object-Oriented Application Development Using VB.NET 26 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 (Mandatory Relation) Invokes the slip’s AddSlipToDock method –AddSlipToDock method establishes the association in both directions by Public Sub AddSlipToDock( ByVal aDock As Dock) aDock.addSlip(Me) theDock. addSlip(Me) setDock(aDock) End Sub A TellAboutSelf method returns information about the slip & the slip’s dock.

27 Object-Oriented Application Development Using VB.NET 27 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, and SetBoat) –Modifications to the Boat class A Slip reference attribute Accessor methods (GetSlip, and SetSlip) AssignBoatToSlip method to establish the relation when needed. –Because of the relation between boat & slip is optional, there is no a slip, or boat parameter passed to the constructor methods.

28 Object-Oriented Application Development Using VB.NET 28 Adding the Boat and Customer Classes to the Slip Example If you get a Customer reference, you Can navigate to find: - The customer’s boat The boat’s slip The slip’s dock If you get a Dock reference, you Can navigate to Each slip The slip’s boat The customer who owns the boat

29 Object-Oriented Application Development Using VB.NET 29 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

30 Object-Oriented Application Development Using VB.NET 30 Creating and Using an Association Class – Lease Modifications to the Lease superclass –A Slip reference attribute Private theSlip As Slip –A Customer reference attribute Private theCustomer As Customer –Accessor methods Both DailyLease & AnnualLease classes –Inherit the association between Lease and Slip Lease and Customer Modifications to the Customer class –A Lease reference attribute Private theLease As Lease –Accessor methods

31 Object-Oriented Application Development Using VB.NET 31 Creating and Using an Association Class – Lease Modifications to the Slip class –A Lease reference attribute Private theLease As Lease –Accessor methods –LeaseAnnualSlip method to instantiate Annual Lease Object. Public Sub LeaseAnnualSlip(ByVal aCus As Customer, _ ByVal startDate As DateTime, ByVal isPM As Boolean ) theLease = New AnnualLease(startDate, slipWidth, isPM) aCus.setLease(theLease) theLease.setSlip(Me) theLease.setCustomer(aCus) End Sub


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

Similar presentations


Ads by Google