Implementation

[Previous] [Next]

Implementing a Singleton design pattern in Visual Basic is not possible with the current language features. You can, however, implement the Singleton design pattern by using the Object Factory design pattern (Chapter 8), as indicated by the TargetFactory class in the "Object Model" section of this chapter. This implementation in and of itself is not enough to prevent violation of the Singleton design pattern. Any member of the project, whether a form, a module, or a class, can define a method that creates any number of instances for a given class.

To enforce the Singleton design pattern for a target class, follow these steps in Visual Basic:

  1. Create a Microsoft ActiveX component in which only the target class and the object factory for the target class exist. For the best possible performance, the ActiveX component should be an ActiveX DLL; this will be created with a single class module by default.
  2. Rename the class module for your object factory to an appropriate name that represents your target class. You will return to this module to define its CreateInstance method.
  3. Add a class module for the target class.
    • Rename the target class with an appropriate name.
    • Define its properties and methods.
    • Set its Instancing property value to PublicNotCreatable. Doing so will prevent clients from creating instances of this target class.

  4. Activate the object factory.
  5. Ensure the object factory's Instancing property value is set to MultiUse. This will allow clients to create instances of the object factory class.
  6. Now that the target class is defined, you can implement the CreateInstance method in the object factory class for creating instances of the target class. Calling the object factory's CreateInstance method is the only means by which a client can create an instance of the target class; hence, the Singleton design pattern is enforced. The target class object factory controls the number of target class instances permitted. But an important question should come to mind: If the user is allowed to create multiple instances of the object factory class, wouldn't each factory have its own unique target instance that it controls, which violates the intent of the Singleton design pattern? This would in fact be the case if a member of the object factory class were holding the reference to the target class instance.

As described in Chapter 2, classes defined in an ActiveX component project have global access to object variables publicly declared in other modules that reside in that same project. Storing a reference to the target class instance in a global object variable would prevent each object factory from holding its own reference to the target, thereby solving this problem. The following code extract depicts a possible implementation.

 ' Globals module Public g_Target As Target  ' Possible implementation of the CreateInstance method in the ' TargetFactory class Public Function CreateInstance() As Target If g_Target Is Nothing Then Set g_Target = New Target End If Set CreateInstance = g_Target End Function 

Regardless of the number of object factory class instances that reside in a client process, the one Singleton target class instance still remains, as shown in Figure 10-1.

click to view at full size.

Figure 10-1. A client process with multiple TargetFactory class instances that share the same global object reference variable to a single Target class instance.



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