Sample Application

[Previous] [Next]

To convey the benefits and applicability of the Object Factory design pattern, I have decided to build on the automobile example described in the Implementation section of this chapter by creating a virtual showroom application for BSTAM automobiles, shown in Figure 8-4. Customers who can't make it to the nearest dealer for a test drive can enter the virtual showroom where they can check out the latest models and go on a virtual test drive.

click to view at full size.

Figure 8-4. The Unofficial BSTAM Automobile Virtual Showroom sample application demonstrates the use of the Object Factory design pattern.

In this sample application, the customer has a choice of test driving a BSTAM 576i, 586i, or S5. BSTAM manufactures an extensive line of models that continues to grow and change from year to year. In order to reflect these changes in the virtual showroom, the system is designed as shown in Figure 8-5. Notice that all components directly related to the Object Factory design pattern are defined in the ActiveX DLL project BSTAMPlant. To address changes to the product line, new concrete StammerFactory and Stammer classes are added to the BSTAMPlant project, which is then rebuilt and redeployed to the Microsoft Windows workstations running the BSTAMShowroom Standard EXE application. BSTAMShowroom is a client of BSTAMPlant. What follows is a brief depiction of the components that play a key role in this implementation of the Object Factory design pattern. Refer to the sample code on the companion CD for a full disclosure.

click to view at full size.

Figure 8-5. Class diagram for the Object Factory design pattern as implemented by the sample application Unofficial BSTAM Automobile Virtual Showroom.

Key components defined in the BSTAMPlant ActiveX DLL project include:

  • Stammer (Product) This interface is an abstraction of the product expected by the client, BSTAMShowroom. The client will reference objects that support the Stammer interface.
  •  ' Project: BSTAMPlant ' File Name: Stammer.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: Stammer Public Property Get ModelNumber() As String End Property Public Property Get Engine() As String End Property Public Function Accelerate(ToMph As Long) As String End Function  

  • BSTAM576i, BSTAM586i, and BSTAMS5 (ConcreteProduct) These are concrete classes that implement the Stammer interface. Although all three classes are of type Stammer, they are clearly unique in implementation, which translates as different products in the client application (BSTAMShowroom).
  •  ' Project: BSTAMPlant ' File Name: BSTAM576i.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: BSTAM576i Implements Stammer Private Property Get Stammer_ModelNumber() As String Stammer_ModelNumber = "576i" End Property Private Property Get Stammer_Engine() As String Stammer_Engine = "193hp 2.8-liter in-line six" End Property  ' File Name: BSTAM586i.cls ' Class Name: BSTAM586i Implements Stammer Private Property Get Stammer_ModelNumber() As String Stammer_ModelNumber = "586i" End Property Private Property Get Stammer_Engine() As String Stammer_Engine = "282hp 4.4-liter 32-valve V8" End Property  

  • StammerFactory (Factory) This interface's most important functionality is that it defines the method for creating Stammer objects based on a ModelId parameter, making this a parameterized object factory interface. This interface also defines a method that returns a list of a valid model ids that the client application will depend on to dynamically update its user interface and to make requests back to a ConcreteFactory object to construct an instance of a particular model.
  •  ' Project: BSTAMPlant ' File Name: StammerFactory.cls ' Copyright: 2/99 Bill Stamatakis ' Class Name: StammerFactory Public Function GetModelIds() As String() End Function Public Function CreateInstance(ModelId As String) As Stammer End Function  

  • FiveSeriesFactory (ConcreteFactory) Concrete class that implements the StammerFactory interface. This class's most important feature is that it defines behavior for creating instances of the concrete classes BSTAM576i, BSTAM586i, and BSTAMS5, which support (implement) the Stammer interface. The class is obviously a factory that manufactures 5-Series sedan objects. As shown in the following code extract, the desired concrete class is instantiated based on the ModelId parameter it receives from a given client.
  •  ' Project: BSTAMPlant ' Copyright: 2/99 Bill Stamatakis ' Class Name: FiveSeriesFactory Implements StammerFactory  Private Function StammerFactory_CreateInstance(ModelId As _ String) As Stammer ' Construct the approriate Stammer based on the ModelId ' provided. Select Case ModelId Case "576i" Set StammerFactory_CreateInstance = New BSTAM576i Case "586i" Set StammerFactory_CreateInstance = New BSTAM586i Case "S5" Set StammerFactory_CreateInstance = New BSTAMS5 Case Else Set StammerFactory_CreateInstance = Nothing ' Raising an exception in this case is advisable. ' Err.Raise ... End Select End Function 

Key components defined outside the BSTAMPlant ActiveX DLL project include the BSTAMShowroom (Client). This Standard EXE project contains the client code that defines the usage of the Stammer objects. It in essence provides a virtual exhibit of various BSTAM (Stammer) models, and lets the user take one on a test drive via a Windows user interface. The application is designed so that it can dynamically recognize the addition of new Stammer models and the removal of discontinued models from the virtual showroom without a single required code change to the client. This is possible because of the Object Factory design pattern implemented by the BSTAMPlant ActiveX DLL that serves up StammerFactory and Stammer objects. The client's only concern is to obtain a reference to an object that supports the StammerFactory interface through which it invokes the CreateInstance method, passing the method a unique model id to create and return a reference to a specific object that supports the Stammer interface. How does the client know what the valid model ids are? The client queries the StammerFactory object for the list of valid model ids, which it then stores in the lstModels list box control. When the user clicks on a model id in the list box control, the list box Click event is fired. As a result the corresponding lstModels_Click event handler is invoked. The event handler contains the code to retrieve the currently selected item in the list that contains a valid model id. It uses this id as a parameter to the StammerFactory.CreateInstance method.

 Private Sub lstModels_Click() Dim strModelId As String  ' Get models from currently selected item in the ' lstModels list box control. strModelId = lstModels.Text  Set m_Stammer = m_StammerFact.CreateInstance(strModelId)  



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