I have revised the Object Factory sample application discussed in Chapter 8 to create a framework that implements the Prototypical Object Factory design pattern. Like the Object Factory sample, this sample application is a BSTAM virtual automobile showroom that let's you take all available automobiles on a virtual test drive. In Chapter 8 the various models that appear in the list box on the user interface are retrieved by code that queries a specific factory for a list of model ids that correspond to the classes of objects that the factory can create. The disadvantage of the Object Factory design pattern implementation is that the factory's knowledge of classes that support the interface that the factory expects is static. If you want to add other classes of automobiles to the application you must have access to the factory source code.
Let's assume the application was provided by BSTAM to its dealerships. Anytime BSTAM adds a new model to its product line, BSTAM sends out an updated application. No problem. Everything works as it should. However, let's also assume that a particular dealership does some after-market enhancements to the BSTAM automobiles and would like to make those vehicles available for test drives in the BSTAM virtual showroom application. In order to address this demand, BSTAM decides to enhance the application by implementing the Prototypical Object Factory design pattern to allow dealerships to add customized automobiles to the virtual showroom. What follows is a brief synopsis of the participants that form the Prototypical Object Factory design pattern. For a complete disclosure of the source code, refer to the companion CD.
The BSTAM virtual showroom application consists of a framework library that resides in the ActiveX DLL (BSTAMFx.DLL) and a Windows front end developed as a Standard EXE (BSTAMShowroom.EXE). Most of the applica-tion behavior originates from the framework library. The BSTAMShowroom is a thin client process that interacts with BSTAMFx, which is loaded in the client's address space. Custom Stammer automobile classes are defined in the BSTAMShowroom project. Instances of these classes are registered with specific factories in the framework, thus extending the framework behavior (or feature set). The following list describes the class modules contained in the BSTAM virtual showroom application that correspond to the classes and interfaces required by the Prototypical Object Factory design pattern.
' Project: BSTAMFx ' File Name: Stammer.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: Stammer Public Function Clone() As Stammer End Function |
' Project: BSTAMShowroom ' File Name: Stam5.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: Stam5 Implements BSTAMFx.Stammer Private Function Stammer_Clone() As BSTAMFx.Stammer Set Stammer_Clone = New Stam5 End Function |
' Project: BSTAMFx ' File Name: StammerFactory.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: StammerFactory Public Function CreateInstance(ModelId As String) As Stammer End Function Public Function AddCustomStammer(ByVal Stammer As Stammer) _ As Boolean End Function Public Function RemoveCustomStammer(ModelId As String) _ As Boolean End Function |
' Project: BSTAMFx ' File Name: FiveSeriesFactory.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: FiveSeriesFactory Implements StammerFactory Private Function StammerFactory_CreateInstance(ModelId _ As String) As Stammer Dim CustomStammer As Stammer ' If the prototypical Stammer object exists in the ' registered list, instruct the object to clone itself ' and return a reference to the clone; otherwise ' return Nothing and raise an error. If m_CustomStammers.Exists(ModelId) Then Set CustomStammer = m_CustomStammers(ModelId) Set StammerFactory_CreateInstance = _ CustomStammer.Clone() Else Set StammerFactory_CreateInstance = Nothing ' Raising an exception in this case is advisable. ' Err.Raise ... End If End Function Private Function StammerFactory_AddCustomStammer _ (ByVal Stammer As Stammer) As Boolean Dim strModelNum As String strModelNum = Stammer.ModelNumber ' Register prototypical objects. If Not m_CustomStammers.Exists(strModelNum) Then m_CustomStammers(strModelNum) = Stammer StammerFactory_AddCustomStammer = True Else StammerFactory_AddCustomStammer = False End If End Function Private Function StammerFactory_RemoveCustomStammer _ (ModelId As String) As Boolean ' Unregister prototypical objects. If m_CustomStammers.Exists(ModelId) Then m_CustomStammers.Remove ModelId End If StammerFactory_RemoveCustomStammer = True End Function |