1 / 39

Chapter 9

Chapter 9. Implementing Association Relationships. 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. Objectives. In this chapter, you will:

abena
Télécharger la présentation

Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 Implementing Association Relationships 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 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 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 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 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) Object-Oriented Application Development Using VB .NET

  7. Identifying Association Relationships on Bradshaw Marina’s Class Diagram 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 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 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 Object-Oriented Application Development Using VB .NET

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

More Related