390 likes | 398 Views
Learn how to identify and implement association relationships in object-oriented application development using VB.NET. This chapter covers concepts such as one-to-one and one-to-many relationships, adding functionality to classes, and creating and using association classes.
E N D
Chapter 9 Implementing Association Relationships Object-Oriented Application Development Using VB .NET
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
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
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
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
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
Identifying Association Relationships on Bradshaw Marina’s Class Diagram Object-Oriented Application Development Using VB .NET
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
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
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
Associating VB .NET Classes in a One-to-One Relationship Object-Oriented Application Development Using VB .NET
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
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
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
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
Modifying the Customer Class • Constructor • Sets the Boat reference variable to Nothing using the SetBoat method • في الكونستراكتر تبع الزبون ، نضع في الاتربيوت بوت ولاشيءNOTHING • Once Customer instance is created • عندما ننشئ الزبون • Boat reference variable can be set to an actual Boat reference • عن طريق السيت نسند له قيمته Object-Oriented Application Development Using VB .NET
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 نضيف ميثود ربط البوت بالسليب • لايمكن وضع علاقه ون تو ون اجباري في الجهتين ، لابد ان يكون احدهما اختياري 0،1 او كلاهما Object-Oriented Application Development Using VB .NET
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
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
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
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
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
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
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
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