1 / 16

23 – Object Associations

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

Télécharger la présentation

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

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

  3. Example: Face Class Properties Class Methods Option Explicit Public x As Single Public y As Single Public Sub Draw(picDest As PictureBox) picDest.Cls picDest.Circle (x, y), 500 picDest.Circle (x - 150, y - 100), 100 picDest.Circle (x + 150, y - 100), 100 picDest.Line (x - 200, y + 300)-(x + 200, y + 300) picDest.Line (x, y - 50)-(x, y + 150) End Sub Public Sub MoveUp() y = y - 100 End Sub

  4. Example: Face Form Option Explicit Dim tmpFace As Face Private Sub Form_Load() Set tmpFace = New Face tmpFace.x = 2000 tmpFace.y = 2000 Me.Show tmpFace.Draw Me.picMain End Sub Private Sub btnUp_Click() tmpFace.MoveUp tmpFace.Draw Me.picMain End Sub Private Sub Form_Unload(Cancel As Integer) Set tmpFace = Nothing End Sub Face

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

  6. Example: 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

  7. Example: 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:

  8. Example: Bar (Implementation) Class Form Class Bar

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

  10. Example: 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: 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: 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: 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: 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

  15. Tutorial Exercise: Bar • Task 1: Get the Bar example from the lecture working. • Task 2: Modify your code – add code to calculate the cost of the order. This object method is not in the lecture notes – you need to create it (not necessarily on your own – discuss it with others, feel free to ask me for help). • Task 3: What happens if the user tries to add more than 10 drinks? Modify your code to cope with this (you decide how it should respond). • Task 4: What happens if the user tries to add a drink when none is selected (in the drinks list box)? Modify your code to cope with this. • Task 5: What happens if the user tries to remove a drink when none is selected (in the order list box)? Modify your code to cope with this. • Task 6: Modify your code – so that the cost is continuously calculated and there is no need for the cost button.

  16. Tutorial Exercise: Music • Task 1: Implement a music track program that stores Track title, and duration, and artist name. Use object-oriented techniques.

More Related