Sample Application

[Previous] [Next]

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.

  • Stammer (Prototype) Defined in the BSTAMFx.DLL project, this interface is an abstraction of the products manufactured by the framework object factories. Most notably, this interface defines a Clone method to permit classes that implement this interface to provide behavior for cloning instances of themselves.
  •  ' Project:      BSTAMFx ' File Name:    Stammer.cls ' Copyright:    2/99 Bill Stamatakis ' Class Name:   Stammer Public Function Clone() As Stammer End Function  

  • Stam5 (ConcretePrototype) Defined in the BSTAMShowroom.EXE project, this class implements the Stammer interface. As the following code extract illustrates, this class provides implementation for the Clone method that allows an instance of this class to clone itself.
  •  ' 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  

  • StammerFactory (ObjectFactory) Defined in the BSTAMFx.DLL project, this interface defines methods that facilitate the implementation of the Prototypical Object Factory design pattern. As you can see from the object model for this design pattern (see the "Object Model" section of this chapter), defining a separate factory interface such as this is not required to make effective use of this design pattern. However, in the case of this application, defining an object factory interface allows the application to scale with less effort.
  •  ' 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  

  • FiveSeriesFactory (ConcreteFactory) Defined in the BSTAMFx.DLL project, this class implements the StammerFactory interface, providing the behavior necessary to register prototypical instances of objects derived from the Stammer interface (such as Stam5, described previously). This class has implemented the StammerFactory.CreateInstance method to instruct these objects to create instances of themselves. (The objects create these instances of themselves by calling their implementations of the Stammer.Clone method.) The objects then return references to the clones to the calling procedure.
  •  ' 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  



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