Flylib.com

Books Software

 
 
 

Sample Application

[Previous] [Next]

Sample Application

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):
  • 'ClassName:Map
    'Note:Thisisanabstractinterfacefromwhich
    'concreteclasseswillinherit.
    
    OptionExplicit
    
    PropertyGetCount()AsLong
    EndProperty
    
    PublicSubAdd(KeyAsString,ItemAsVariant)
    EndSub
    
    PublicFunctionGetKeys(Keys()AsString)AsLong
    EndFunction
    
    PublicFunctionItem(KeyAsString)AsVariant
    EndFunction
    
    PublicSubRemove(KeyAsString)
    EndSub
    
  • 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:
  • 'ClassName:SMap
    'Note:ThisisaconcreteMapclass.
    'Itmaintainsamapcollection
    'sortedinkeyorder.
    
    OptionExplicit
    
    'InheritstheMapinterface
    ImplementsMap
    
    
  • 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:
  • 'ClassName:SMap
    
    OptionExplicit
    
    'InheritstheMapinterface
    ImplementsMap
    
    Privatem_collectionAsCollection
    
    'Add
    PrivateSubMap_Add(KeyAsString,ItemAsVariant)
    m_collection.AddKey:=Key,Item:=Item
    'Addkeyinsortorder
    AddKeyKey
    EndSub
    
    'Count
    PrivatePropertyGetMap_Count()AsLong
    Map_Count=m_collection.Count
    EndProperty
    
    'Item
    PrivateFunctionMap_Item(KeyAsString)AsVariant
    IfVarType(m_collection.Item(Key))=vbObjectThen
    SetMap_Item=m_collection.Item(Key)
    Else
    Map_Item=m_collection.Item(Key)
    EndIf
    EndFunction
    
    'Remove
    PrivateSubMap_Remove(KeyAsString)
    m_collection.RemoveKey
    m_keys.RemoveKey
    EndSub
    

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.