23 Object Associations Mark Dixon So CCE SOFT
23 – Object Associations Mark Dixon, So. CCE SOFT 131 Page 1
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 Mark Dixon, So. CCE SOFT 131 Page 2
Example: Face Class Option Explicit Properties Public x As Single Public y As Single Methods Public Sub Draw(pic. Dest As Picture. Box) pic. Dest. Cls pic. Dest. Circle (x, y), 500 pic. Dest. Circle (x - 150, y - 100), 100 pic. Dest. Circle (x + 150, y - 100), 100 pic. Dest. Line (x - 200, y + 300)-(x + 200, y + 300) pic. Dest. Line (x, y - 50)-(x, y + 150) End Sub Class Public Sub Move. Up() y = y - 100 End Sub Mark Dixon, So. CCE SOFT 131 Page 3
Example: Face Form Option Explicit Dim tmp. Face As Face Private Sub Form_Load() Set tmp. Face = New Face tmp. Face. x = 2000 tmp. Face. y = 2000 Me. Show tmp. Face. Draw Me. pic. Main End Sub Private Sub btn. Up_Click() tmp. Face. Move. Up tmp. Face. Draw Me. pic. Main End Sub Face Mark Dixon, So. CCE Private Sub Form_Unload(Cancel As Integer) Set tmp. Face = Nothing End Sub SOFT 131 Page 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 Mark Dixon, So. CCE SOFT 131 Page 5
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 Mark Dixon, So. CCE SOFT 131 Page 6
Example: Bar (Design v 1) • Object Classes, properties, and methods – Class diagram: Order m. Drinks(): Drink Add(long, long) Remove(long) Display (List. Box) Cost(): double Mark Dixon, So. CCE Drink m. Type: Long m. Qty: Long SOFT 131 Page 7
Example: Bar (Implementation) Class Form Class Bar Mark Dixon, So. CCE SOFT 131 Page 8
Example: Bar (Drink module) • Drink (class module): Drink m. Type: Long m. Qty: Long Mark Dixon, So. CCE Option Explicit Public m. Type As Long Public m. Qty As Long SOFT 131 Page 9
Example: Bar (Order module) Option Explicit Order m. Drinks(): Drink Add(long, long) Remove(long) Display (List. Box) Cost(): double Mark Dixon, So. CCE Const First = 0 Const Last = 9 Private m. Drinks(First To Last) As Drink Private m. List. Box As List. Box Public Sub Add(tmp. Type As Long, tmp. Qty As Long) Dim d As Long ' Find free slot. For d = First To Last If m. Drinks(d) Is Nothing Then Exit For End If Next ' Create object and store data. Set m. Drinks(d) = New Drink m. Drinks(d). m. Type = tmp. Type m. Drinks(d). m. Qty = tmp. Qty SOFT 131 Page 10
Example: Bar (Order module) • method (procedure) to display order's drinks in a list box Order m. Drinks(): Drink Add(long, long) Remove(long) Display (List. Box) Cost(): double Mark Dixon, So. CCE Public Sub Display(lst. Order As List. Box) Dim d As Long Dim tmp. Str As String lst. Order. Clear For d = First To Last If Not (m. Drinks(d) Is Nothing) Then tmp. Str = m. Drinks(d). m. Qty & " " tmp. Str = tmp. Str & m. List. Box. List(m. Drinks(d). m. Type) lst. Order. Add. Item tmp. Str lst. Order. Item. Data(lst. Order. New. Index) = d End If Next End Sub SOFT 131 Page 11
Example: Bar (Order module) • method (procedure) to remove drink from order Order m. Drinks(): Drink Add(long, long) Remove(long) Display (List. Box) Cost(): double Mark Dixon, So. CCE Public Sub Remove(d As Long) Set m. Drinks(d) = Nothing End Sub SOFT 131 Page 12
Example: Bar (Order module) • Method to populate Drinks List Public Sub Drinks. List. Init(tmp. List As List. Box) Set m. List. Box = tmp. List m. List. Box. Clear m. List. Box. Add. Item "Coke", 0 m. List. Box. Item. Data(0) = 115 m. List. Box. Add. Item "Lemonade", 1 m. List. Box. Item. Data(1) = 110 m. List. Box. Add. Item "Beer", 2 m. List. Box. Item. Data(2) = 180 m. List. Box. Add. Item "Cider", 3 … m. List. Box. Add. Item "Whisky", 6 m. List. Box. Item. Data(6) = 95 m. List. Box. Add. Item "Rum", 7 m. List. Box. Item. Data(7) = 105 End Sub Mark Dixon, So. CCE SOFT 131 Page 13
Example: Bar (frm. Main module) Option Explicit Private cur. Order As Order Private Sub Form_Load() Set cur. Order = New Order cur. Order. Drinks. List. Init lst. Drinks End Sub Private Sub btn. Add_Click() cur. Order. Add lst. Drinks. List. Index, txt. Qty. Text cur. Order. Display lst. Order End Sub Private Sub btn. Remove_Click() cur. Order. Remove lst. Order. Item. Data(lst. Order. List. Index) cur. Order. Display lst. Order End Sub Mark Dixon, So. CCE Private Sub btn. Cost_Click() lbl. Cost. Caption = "£" & cur. Order. Cost End Sub SOFT 131 Page 14
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. Mark Dixon, So. CCE SOFT 131 Page 15
Tutorial Exercise: Music • Task 1: Implement a music track program that stores Track title, and duration, and artist name. Use object-oriented techniques. Mark Dixon, So. CCE SOFT 131 Page 16
- Slides: 16