In an ActiveX DLL project named Adapters (located in the Adapter directory on the companion CD), I have implemented fully the SMap collection described in Scenario A of this chapter, which is an Opaque Object Adapter design pattern. What follows is a brief compilation of all participants in the sample application. Please refer to the companion CD for the full source code.
' Class Name: Map ' Note: This is an abstract interface from which ' concrete classes will inherit. Option Explicit Property Get Count() As Long End Property Public Sub Add(Key As String, Item As Variant) End Sub Public Function GetKeys(Keys() As String) As Long End Function Public Function Item(Key As String) As Variant End Function Public Sub Remove(Key As String) End Sub |
' Class Name: SMap ' Note: This is a concrete Map class. ' It maintains a map collection ' sorted in key order. Option Explicit ' Inherits the Map interface Implements Map |
' Class Name: SMap Option Explicit ' Inherits the Map interface Implements Map Private m_collection As Collection ' Add Private Sub Map_Add(Key As String, Item As Variant) m_collection.Add Key:=Key, Item:=Item ' Add key in sort order AddKey Key End Sub ' Count Private Property Get Map_Count() As Long Map_Count = m_collection.Count End Property ' Item Private Function Map_Item(Key As String) As Variant If VarType(m_collection.Item(Key)) = vbObject Then Set Map_Item = m_collection.Item(Key) Else Map_Item = m_collection.Item(Key) End If End Function ' Remove Private Sub Map_Remove(Key As String) m_collection.Remove Key m_keys.Remove Key End Sub |
This example clearly illustrates the major advantage of an Adapter design pattern, which is its ability to define a new type of object that makes effective use of existing system components.
NOTE
You can use the AdapterTester project, located in the Adapter\AdapterTester subdirectory on the companion CD, to test the Adapters DLL.