1 / 14

15 – Object Associations

15 – Object Associations. Admin. Assignment 1 Tutorials attendance you are not asking enough questions you are not completing all exercises (especially those at the end – more important). Session Aims & Objectives. Aims

leane
Download Presentation

15 – Object Associations

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. 15 – Object Associations

  2. Admin • Assignment 1 • Tutorials • attendance • you are not asking enough questions • you are not completing all exercises(especially those at the end – more important)

  3. Session Aims & Objectives • Aims • To introduce some of the more subtle aspects of object oriented design (such as object associations) • Objectives,by end of this week’s sessions, you should be able to: • create a project with several associated objects

  4. Object Associations • In practice projects will be made of • many object classes • that interact with each other (are associated) • There are several types of association • One of the most often used is the ‘part of’ association, • where one object class forms part of another object class • A common example of this occurs where it is necessary to store multiple instances of a class

  5. Example 1: Bar (Analysis) The students' Union bar needs a computer system for recording the purchase of drinks. Typically, a student will stagger to the bar and describe their order, consisting of one or (usually) more drinks. The bar staff will then prepare the drinks and calculate the cost of the order. • Scenario 1: small project, limited automation • Nouns: drinks, order, cost • Verbs: describe, calculate cost

  6. Example 1: Bar (Design v1) Order Drink mDrinks(): Drink mType: Long mQty: Long Add(long, long) Remove(long) Display (ListBox) Cost(): double • Object Classes, properties, and methods • Class diagram:

  7. Example 1: Bar (Implementation) Class Form Class Bar

  8. Example 1: Bar (frmMain module) Option Explicit Private curOrder As Order Private Sub Form_Load() Set curOrder = New OrdercurOrder.DrinksListInit lstDrinks End Sub Private Sub btnAdd_Click() curOrder.Add lstDrinks.ListIndex, txtQty.Text curOrder.Display lstOrder End Sub Private Sub btnRemove_Click() curOrder.Remove lstOrder.ItemData(lstOrder.ListIndex) curOrder.Display lstOrder End Sub Private Sub btnCost_Click() lblCost.Caption = "£" & curOrder.Cost End Sub

  9. Example 1: Bar (Drink module) Drink mType: Long mQty: Long • Drink (class module): Option Explicit Public mType As Long Public mQty As Long

  10. Example 1: Bar (Order module) Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double Option Explicit Const First = 0 Const Last = 9 Private mDrinks(First To Last) As Drink Private mListBox As ListBox Public Sub Add(tmpType As Long, tmpQty As Long) Dim d As Long ' Find free slot. For d = First To Last If mDrinks(d) Is Nothing Then Exit For End If Next ' Create object and store data. Set mDrinks(d) = New Drink mDrinks(d).mType = tmpType mDrinks(d).mQty = tmpQty End Sub

  11. Example 1: Bar (Order module) Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double • method (procedure) to display order's drinks in a list box Public Sub Display(lstOrder As ListBox) Dim d As Long Dim tmpStr As String lstOrder.Clear For d = First To Last If Not (mDrinks(d) Is Nothing) Then tmpStr = mDrinks(d).mQty & " " tmpStr = tmpStr & mListBox.List(mDrinks(d).mType) lstOrder.AddItem tmpStr lstOrder.ItemData(lstOrder.NewIndex) = d End If Next End Sub

  12. Example 1: Bar (Order module) Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double • method (procedure) to remove drink from order Public Sub Remove(d As Long) Set mDrinks(d) = Nothing End Sub

  13. Example 1: Bar (Order module) • Method to populate Drinks List Public Sub DrinksListInit(tmpList As ListBox) Set mListBox = tmpList mListBox.Clear mListBox.AddItem "Coke", 0 mListBox.ItemData(0) = 115 mListBox.AddItem "Lemonade", 1 mListBox.ItemData(1) = 110 mListBox.AddItem "Beer", 2 mListBox.ItemData(2) = 180 mListBox.AddItem "Cider", 3 … mListBox.AddItem "Whisky", 6 mListBox.ItemData(6) = 95 mListBox.AddItem "Rum", 7 mListBox.ItemData(7) = 105 End Sub

  14. Example 1: Bar (Design v2) Order Drink mDrinks(): Drink mListBox: ListBox mType: Long mQty: Long DrinksListInit()Add(long, long) Remove(long) Display (ListBox) Cost(): double • Object Classes, properties, and methods:

More Related