As in Visual Basic, passing a COM object by value in C++ is not a built-in language feature but is a feature the majority of the COM programming community would like to see added to the Microsoft COM specification. I hope it will happen soon and, since Visual Basic object-oriented programming is deeply rooted in COM, the feature will eventually find its way into Visual Basic. In the interim, the Object By Value design pattern can allow you to implement this feature today.
The Object By Value design pattern, similar to the Prototypical Object Factory (Chapter 9),
Chapter 7
What can you surmise from this scenario?
Ms. Smart Proxy says, "I accept this award on
behalf of Bill Stamatakis, who could not be here tonight because he is on a business trip in Tokyo. Bill wanted me to relay his sincerest gratitude for this gesture and to thank the following people for their contributions that lead to this award…"
Based on this excerpt, you can guess that Ms. Smart Proxy represented Bill Stamatakis at an awards ceremony by accepting a prize intended for him, and communicated his
Ms. Smart Proxy's role was therefore important in keeping the award presentations
Replacing the word "person" with "object" allows us to view the concept of a proxy from the perspective of object-oriented software development.
The Smart Proxy design pattern provides a reference to a surrogate object that represents a real object in a given context.
[Previous] [Next]The Smart Proxy design pattern is useful in the following circumstances:
To understand when to
Typically an object's state and behavior are represented as properties and
Dimperson1AsHRComponent.Person 'ConstructaPersonobjectandretainareferencetotheperson1 'objectvariable. Setperson1=NewPerson 'InitializethestateofthePersonobject. Withperson1 .Name="Smith" .Age=32 .Height="5-10" EndWith |
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
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
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
Dimperson1AsHRComponent.Person Dimattribs(0To1,0To2)AsVariant 'ConstructaPersonobjectandreturnareferencetotheperson1 'objectvariable. Setperson1=NewPerson 'SetupattributesforsubmissiontoPersonobject: ' 'Initializeheaderofnames. attribs(0,0)="Name" attribs(0,1)="Age" attribs(0,2)="Height" 'Initializevaluespername. attribs(1,0)="Smith" attribs(1,1)=32 attribs(1,2)="5-10" 'SendUpdateStaterequesttoPersonobject. Callperson1.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
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
Figure 7-1. Smart Proxy design pattern implementation of the Person class.
Dimperson1AsHRComponent.SmartProxyPerson 'ConstructaSmartProxyPersonobjectandreturnareferencetothe 'person1objectvariable. ' 'Duringinitialization,SmartProxyPersonconstructsadumbproxy 'Personobjecttowhichitultimatelydelegatesallrequests 'intendedfortherealPersonobjectthatresidesoutsidethe 'addressspaceoftheclientinitiatingtherequest. ' Setperson1=NewSmartProxyPerson 'Setthepropertyvalues. person1.Name="Smith" person1.Age=32 person1.Height="5-10" 'Updatethestateoftherealpersonobject. person1.SetComplete |