140 likes | 230 Views
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
E N D
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 • 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
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
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
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:
Example 1: Bar (Implementation) Class Form Class Bar
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
Example 1: Bar (Drink module) Drink mType: Long mQty: Long • Drink (class module): Option Explicit Public mType As Long Public mQty As Long
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
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
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
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
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: