Microsoft Visual Basic Design Patterns (Microsoft Professional Series)

[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.

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.

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:

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)

Категории