Sample Application

[Previous] [Next]

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.

  • Map, the interface expected by clients, is an abstract interface that defines how objects of any type are stored in a collection. It maps a key of type String to the object's location in the collection. So instead of traversing through a collection for the object required, you use the object's key for easy and instant retrieval. Because Map is an abstract interface, I created a class module named Map. Then I changed its Instancing property value to PublicNotCreatable, which means you will not be able to create instances of the Map type outside this project. You can declare object variables of type Map without receiving an error, but you will not be able to use the keyword New or the CreateObject function to create an instance. As the following code extract illustrates, the Map class module contains property and function definitions with no implementation (empty function bodies):
  •  ' 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 

  • SMap (Adapter) is a concrete Map class that implements the interface defined by Map. Said another way, SMap is a type of Map. SMap also has the added functionality of maintaining the collection in sorted order by key, hence the S prefix:
  •  ' 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  

  • The important functionality contained in SMap is its maintenance of a private object variable that stores a reference to a Visual Basic Collection object. Most of the work necessary to maintain the sorted map collection is delegated to the Collection object via the reference stored in m_collection. Notice that all the functions derived from the Map interface—except for GetKeys—delegate all work to the m_collection object variable:
  •  ' 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.



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net