Sample Application

[Previous] [Next]

In this simple car loan calculator program, the user can view multiple calculators side by side so that he or she can easily compare the differences in monthly loan payments based on the loan amount and the loan interest rate. Even though the user can view multiple instances of the car loan calculator window, the program doesn't need to duplicate instances of the calculator engine. A single Calculator object will be sufficient to serve any number of car loan calculator windows. Let's take a closer look.

Two projects have been created: AutoLoanCalcSvr and AutoLoanCalculator. To gain the full benefit of this section, load both projects into Visual Basic. The AutoLoanCalcSvr project is an ActiveX DLL that implements a Calculator class object as a Singleton design pattern. The Calculator object implements the functionality for calculating the monthly payments of the car loan. To implement the Singleton design pattern, you must perform the steps described in the "Implementation" section of this chapter. Set the Instancing property value of the Calculator class to PublicNotCreatable. Clients (AutoLoanCalculator) are now unable to create instances of this class. Notice that the CalcFactory class (see the Object Factory design pattern, described in Chapter 8) in this project is responsible for creating instances of the Calculator class. Set the CalcFactory class's Instancing property value to MultiUse to allow clients to create instances of it. An instance of a Calculator class can be created only through a CalcFactory object's CreateInstance method. In other words, the CalcFactory object controls the number of Calculator instances permitted. As the following code extract illustrates, the CalcFactory object permits only one instance of the Calculator class. All subsequent calls to CreateInstance result in CalcFactory returning a reference to that same instance of the Calculator. Finally, a minor but important step is to define the object variable g_Calculator used by CalcFactory as a module-level variable. This guarantees that only one reference to the Calculator object is shared by all instances of the CalcFactory class, regardless of the number of instances the client creates. (Refer to the "Implementation" section for a full explanation.)

 ' Class Name: CalcFactory Public Function CreateInstance() As Calculator If g_Calculator Is Nothing Then Set g_Calculator = New Calculator End If Set CreateInstance = g_Calculator End Function 

AutoLoanCalculator is a Standard EXE project. It is a client of the AutoLoanCalcSvr project. If you build this project, it will create an executable that, when run, displays an Auto Loan Calculator window. (See Figure 10-2.) From the File menu, select New Window to launch a new instance of the Auto Loan Calculator window. You can then make side-by-side calculations and compare the results. From a usage standpoint, it might seem that the application is launching another instance of itself: it is not. The one application instance is launching another instance of the window (Visual Basic form). Note that the form contains member object variables that maintain references to a CalcFactory object and a Calculator object. These member object variables are initialized in the form's Form_Initialize event handler function, as shown in the following code extract.

 Private Sub Form_Initialize() Set m_CalcFact = New AutoLoanCalcSvr.CalcFactory Set m_Calculator = m_CalcFact.CreateInstance End Sub 

This event handler is called every time a new form is created. Each form instance maintains its own instances of the object variables m_CalcFact and m_Calculator. Because the Calculator object is a Singleton, you should recognize by looking at this client that each form instance has a unique instance of the CalcFactory class, but that they all share the same instance of the Calculator.

Figure 10-2. The Auto Loan Calculator window.



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