Scenario

[Previous] [Next]

To understand when to employ the Smart Proxy design pattern, consider an example of the first situation described in the "Utilization" section. An out-of-process COM server (ActiveX EXE) is running on a server connected to your company network. Microsoft Visual Basic clients running on workstations connected to the network bind to objects on this server through the COM layer. Because the client and server processes do not reside in the same process address space, a proxy to the real object on the server is automatically returned to the client. The proxy delegates the client's request across the network to the real object. The source code for the client remains the same whether the object being referenced is in-process (ActiveX DLL) or out-of-process (ActiveX EXE), but performance degradation will be evident in an out-of-process scenario. Therefore, the least amount of interaction with the server, the better.

Typically an object's state and behavior are represented as properties and methods in an interface. For example, this code extract illustrates the steps to construct and initialize a Person object.

 Dim person1 As HRComponent.Person ' Construct a Person object and retain a reference to the person1 ' object variable. Set person1 = New Person ' Initialize the state of the Person object. With person1 .Name = "Smith" .Age = 32 .Height = "5-10" End With 

If HRComponent is an ActiveX DLL, the cost of setting each property is minimal because the Person object resides in the client process's address space. The interface of the Person object clearly is self-documenting because the properties distinctly define what the Person object supports. This is good programming style from the perspective of object-oriented design.

If HRComponent is an ActiveX EXE, however, the cost of setting each property value is considerably higher. The Person object does not reside in the client process's address space, so each property assignment is individually delegated across the network to the real object running on the ActiveX EXE server. The object variable person1 has a reference to a proxy provided internally by Visual Basic. This proxy transparently packages and submits each request to the real object. Furthermore, if the code were to request a property value (such as strName = person1.Name), the proxy would forward this request across the network to the real object because the proxy is stateless by default. The proxy's sole purpose is to forward requests to the object that it represents.

You can solve this out-of-process performance problem in two ways: you can modify the interface to support setting a series of properties in one call, or you can employ the Smart Proxy design pattern. Let's examine both approaches.

A common and effective solution is to create an array of name-value pairs, which you can do by creating a two-dimensional array of type Variant, as shown in the following code extract. The first row of the array is the header containing the names. All the following rows contain the values for each name. Performance is superior to the individual property-setting approach, but this method of implementation doesn't provide a true object-oriented solution. The interface supported by the Person object is no longer self-documenting. Debugging and understanding the code becomes significantly more difficult.

 Dim person1 As HRComponent.Person Dim attribs(0 To 1, 0 To 2) As Variant ' Construct a Person object and return a reference to the person1 ' object variable. Set person1 = New Person ' Set up attributes for submission to Person object: ' ' Initialize header of names. attribs(0, 0) = "Name" attribs(0, 1) = "Age" attribs(0, 2) = "Height" ' Initialize values per name. attribs(1, 0) = "Smith" attribs(1, 1) = 32 attribs(1, 2) = "5-10" ' Send UpdateState request to Person object. Call person1.UpdateState(attribs) 

It might be worth sacrificing the principles of object orientation in order to increase performance. Implementing the Smart Proxy design pattern, however, allows you to stick to your principles and at the same time gain the much-needed performance.

A smart proxy is "smart" because, unlike a "dumb" proxy that just forwards all requests to the real object, the smart proxy can decide when it is necessary to contact the real object. For instance, the smart proxy can accumulate and package a series of requests into a compound structure that is submitted to the real object in one call across the wire. Also, a smart proxy can cache the real object's state locally in the client process. Requesting the property value of a smart proxy returns the results as quickly as if the client were accessing the real object in-process. A Smart Proxy design pattern is really a class that wraps the dumb proxy provided within Visual Basic. (See Chapter 4.) The Smart Proxy design pattern in essence implements the same Person interface that both the dumb proxy and real object implement. (See Figure 7-1.) The Smart Proxy design pattern therefore remains transparent to the client without compromising performance or object-oriented principles.

In the Smart Proxy design pattern implementation, you can choose to use a two-dimensional array of type Variant similar to the non-object-oriented solution described previously. Since it is an internal implementation, it would remain private to the class. Setting a property value would update the two-dimensional array defined internally. When you've updated the properties in the smart proxy, follow through with a call to a method that submits all property settings as a single request with an accompanying array. The SmartProxyPerson class submits requests to the dumb proxy that is automatically generated by Visual Basic. For simplicity, let's assume the SmartProxyPerson class is creating an instance and holding onto a reference to the dumb proxy in its Class_Initialize event handler, relieving the client of that task. (The "Implementation" section of this chapter covers the possible implementation options in more detail.) Let's also assume the state is cached locally. When the client requests a property value, it is returned from the SmartProxyPerson object that is in-process. The code extract below is a feasible scenario for updating multiple property values of the real Person object as a single request.

click to view at full size.

Figure 7-1. Smart Proxy design pattern implementation of the Person class.

 Dim person1 As HRComponent.SmartProxyPerson ' Construct a SmartProxyPerson object and return a reference to the ' person1 object variable. ' ' During initialization, SmartProxyPerson constructs a dumb proxy ' Person object to which it ultimately delegates all requests ' intended for the real Person object that resides outside the ' address space of the client initiating the request. ' Set person1 = New SmartProxyPerson ' Set the property values. person1.Name = "Smith" person1.Age = 32 person1.Height = "5-10" ' Update the state of the real person object. person1.SetComplete 



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