A proxy class is a stand-in for another class.
Let's suppose you have a class that has a method that takes 60 seconds to complete. That means everytime you call that method, your program waits. But let's also assume you rarely call that method. Let's further assume this method is named Load() and the class is MyClass
-
class MyClass
-
{
-
public:
-
void Load(); //takes a long time
-
void AMethod();
-
etc... //the other methods.
-
};.
-
The proxy class would look like:
[code=cpp]
class MyClassProxy
{
MyClass* theObject;
public:
MyClassProxy(); : theObject(0) {}
MyClass* operator->();
MyClass& operator*();
};
So when you create a MyClassProxy object, the MyClass* inside is set to zero.
Now you use MyClassProxy objects instead of MyClass objects.
If someone needs the MyClass object, they use the operator-> overload of MyClassProxy. This function just returns the MyClass* if the MyClass object exists otherwise is creates it and calls Load().
-
MyClass* MyClassProxy::operator->()
-
{
-
if (!this->theObject)
-
{
-
theObject = new MyClass;
-
theObject->Load();
-
}
-
return theObject;
-
};
-
So not until you use the proxy object with the -> operator do you see the 60 seccond delay.
-
-
MyClassProxy p; //no delay
-
p->AMethod(); //Here the MyClass object is created
-
//Loaded and the MyClass::AMethod called.
-