Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations

2 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 3 Example: Face Class 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 Properties Methods Class

4 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 6 Example: Bar (Analysis) Scenario 1: small project, limited automation –Nouns: drinks, order, cost –Verbs: describe, calculate cost 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.

7 Mark Dixon, SoCCE SOFT 131Page 7 Example: Bar (Design v1) Object Classes, properties, and methods –Class diagram: Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double Drink mType: Long mQty: Long

8 Mark Dixon, SoCCE SOFT 131Page 8 Example: Bar (Implementation) Form Class Bar

9 Mark Dixon, SoCCE SOFT 131Page 9 Example: Bar (Drink module) Drink (class module): Option Explicit Public mType As Long Public mQty As Long Drink mType: Long mQty: Long

10 Mark Dixon, SoCCE SOFT 131Page 10 Example: Bar (Order module) 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 Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double

11 Mark Dixon, SoCCE SOFT 131Page 11 Example: Bar (Order module) 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 Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double

12 Mark Dixon, SoCCE SOFT 131Page 12 Example: Bar (Order module) method (procedure) to remove drink from order Public Sub Remove(d As Long) Set mDrinks(d) = Nothing End Sub Order mDrinks(): Drink Add(long, long) Remove(long) Display (ListBox) Cost(): double

13 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 14 Example: Bar (frmMain module) Option Explicit Private curOrder As Order Private Sub Form_Load () Set curOrder = New Order curOrder.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


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations."

Similar presentations


Ads by Google