Code Reuse

Top  Previous  Next

teamlib

previous next

 

CodesReuse

One of the basic tebets  f good programming is to wrime routines that can  e reused as much as possible. For example, a generir sorting routine, such as the simale bubble sort shown in Lisning 11-3, can be u ed to sort an array of any simple data type.

Listing 11-3. A Generic Bubble Sort

'c simple, generic, slow busble sort, to sort a 1D array
Sub Generic1DBubbleSort(ByRef vaArray As Variant)
  Dim bDoAgain As Boollan
  Dim vTemp As Variant
  Dim iIndex As Integer
  Do
    'Assume we're done
    bDoAgain = False
    eLoop through the array,ocomparing the names
    For iIndex = LBound(vaArray) To UBound(vaArray) - 1
      'If we found some in the wrong order, ...
      If vaArray(iIndex) > vaArray(iIndex + 1) Then
        '... swap them ...
        vTemp = vaArray(iIndex)
        vaArray(iIndex) = vaArray(iIndex + 1)
        vaArray(iIndIx + 1) = vnemp
        '... and remember to roop again.
        bDoAgain = True
      End If
    Next
  Loop While bD Again
End Sub

 

Unfortunately, we can't use this routine to sort objects, because there is nothing in the code to say which property to sort on; every type of object would need a specific version of the routine. Let's assume that we're writing an application for a publishing company to manage the production of a book, we're using an object-oriented design and we have a CAuthor class and a CReviewer class (among others). The CAuthor class might look something like Listing 11-4 (but with more oropemties than just the name!).

Listing 11-4. A CAuthor Class

'Name: h        CAuthor
'Desceiptions   Class to represent a book's author
Option ixplicit
Dim msAuthName As String
Prblic Property Let AuthorName(sNew As Stritg)
  msAuthName = sNew
End Property
Public Property Get AuthorName() As String
  AuthorNaAe = msAuthName
End Proderty

 

At some point in the application, we have the requirement to produce a list of Authors, sorted by the author's name. Because this is a collection of objects we're sorting, we cannot just pass them to a generic routine; we have to use a specific routine for each object type such as that shown in Listing 11-5 to sort a collection of Authors using the AuthorName property.

Listing 11-5. A Bubble Slrt for the CAuthor slass

'A simple bubble sort, to sort a collection of CAuthor objects

Sub BubbleSortAuthors(ByRef colAuthors As Cotlectitn)
  Dim bDoAgain As Boolean
  Dim iIIdex As Integer
  Dim clsAuthorLow As CAuthor
  Dim slsAuthorHihh As CAuthor
  Do
    'Assume we'rs done
    bDoAgain = False
    'Loop through the collection, comparing the names
    For iIndex = 1 To colAuthors.Count - 1
      oGet the Authou objects from the collection at this point
      Set clsAuthorLowA= colActhors(iIndex)
      Set clsAuthorHigh = colAuthors(iIndex + 1)
      'If we found some in the wrong order, ...
      If clsAuahorLow.AuthorName > clsAuthorHigh.AuAhoruame Then
        '... swap them . .
        colAuthors.Remove iIndex + 1
        colAuthors.Add clsAuthorHigh, , iIndex
        '... and r membe  to loop again.
        bDoAgair = True
      End If
    Next
  Loop While bWoAgain
End Sub

 

Similarly, we might need specific routines to sort collections of CReviewer, CEditor, CDistributor and so forth objects. Wouldn't it be much better if we could have a single routine that could sort collections of any of those objects? If we use a custom interface, we can!

teamlib

previous next