A while back, I started boning up on Software Engineering best practices and
learning about Agile programming. In the process, I've become much more
committed to removing duplication in code at a much finer level. As such,
it's very frustrating to be working in VBA which lacks inheritance, one of the
more powerful tools for eliminating duplication at the level I'm talking
about.
I've recently come up with a technique to emulate one special case of
inheritance that's handy in an awful lot of cases.
First off, yes, it's true that you can just simulate inheritance using
encapsulation, but then you have to add members to every single encapsulating
class to wrap every single public member of the encapsulated class. Each time
you add a member to the "base class" you have to meticulously update all the
"inheriting " classes as well.
So, here's my alternative for one common use case of inheritance. Note that
this is made more useful since other cases of inheritance can be force-fit
into this model. If this is too much to swallow cold, try looking at the code
example at the end, then come back to the text. The code is simpler than the
explanation.
First, the case I'm talking about is where the base class provides functions
and interfaces that are mutable primarily in terms of the metadata they use to
do their jobs. It is not enough, however, to simply initialize an instance
with a data structure, though, because not all the metadata is constant, or
one would not want to count on that being a permanent situation. In a typical
OOP language, this would be done by having private virtual members in the base
class that would be overridden by the inheriting class to supply data to the
base class.
Now, the model. I call this model the "Setup Class" model. It's sort of a
cross between a factory model and a decorator class. Instead of having the
"base class" contained in the "inheriting class", it's the other way around,
but the "inheriting class" (the Setup class) is what the external code creates
first, then uses that as sort of a factory for its base class which is then
stored by the external code, and now encapsulates the Setup class which now
provides the metadata for the Base class.
A simplified code example follows. This is air code, but I have similar code
in production. In my production system, I have one setup object for the
production database, and one for a testing database. The pre-setup for the
testing database makes a new copy from a template, so it is always in a known
state before the download begins. The production database has no pre-setup
action (like this example)...
- Usage example
Option Explicit
Public Sub TestDataReaderA bc()
Dim objDataReader As clsDataReader
' With block creates temporary reference to a new
' clsDataReaderSe tupAbc object, then that is used to
' create and return a new clsDataReader object that
' now encapsulates the clsDataReaderSe tupAbc object,
' and uses it for metadata, etc.
With New clsDataReaderSe tupAbc
Set objDataReader = .GetReader()
End With
With objDataReader
While Not .EndOfData
Debug.Print .DataItem
.NextItem
Wend
End With
End Sub
- clsDataReader
Option Explicit
Private mobjSetup As IDataReaderSetu p
Private mdbs As DAO.Database
Private mrst As DAO.Recordset
Public Sub Setup(objSetup As IDataReaderSetu p)
Dim strSql As String
Set mobjSetup = objSetup
With mobjSetup
.PreSetup
Set mdbs = CurrentDb
strSql = "SELECT " & .FieldName & _
" FROM " & .TableName & _
" ORDER BY " & .FieldName
End With
Set mrst = mdbs.OpenRecord set("",strSql)
End Sub
Public Property Get DataItem() As Variant
DataItem = mrst.Fields(mob jSetup.FieldNam e).Value
End Property
Public Sub NextItem()
mrst.MoveNext
End Property
Public Property Get EndOfData() As Boolean
EndOfData = mrst.EOF
End Property
Private Sub Class_Terminate ()
If Not (mrst Is Nothing) Then
mrst.Close: Set mrst = nothing
End If
Set mdbs = Nothing
End Sub
- IDataReaderSetu p
Option Explicit
Public Sub PreSetup() As clsDataReader
End Function
Public Function GetDataReader() As clsDataReader
End Function
Public Property Get TableName() As String
End Property
Public Property Get FieldName() As String
End Property
- clsDataReaderSe tupAbc (There could have many other Setup classes!)
Option Explicit
Implements IDataReaderSetu p
Public Sub IDataReaderSetu p_PreSetup() As clsDataReader
' No pre-setup actions required for this Setup class
End Function
Private Function IDataReaderSetu p_GetDataReader () As clsDataReader
Dim objResult As New clsDataReader
objResult.Setup Me
Set GetDataReader = objResult
End Function
Private Property Get IDataReaderSetu p_TableName() As String
TableName = "tblTest"
End Property
Private Property Get IDataReaderSetu p_FieldName() As String
TableName = "ItemName"
End Property