So one type of proxy that you're bound to encounter at some point is what's called a virtual proxy now
a virtual proxy gives you the appearance of working with the same object that you're used to working
with.
Even though the object might not have even been created.
So let me show you how this kind of proxy can show up in your code so let's suppose you have some sort
of image interface.
I'm going to have a struct called image and it's going to have a single function called draw virtual
void draw.
It's going to be a pure virtual function.
OK so let's suppose that you come up with some sort of API for having a bitmap.
So we're going to have a bitmap which is going to be an image.
And let's suppose that what happens in that map is you eagerly load the image so the constructor of
the bitmap actually takes the file name and then you proceed to load the bitmap.
So let me show you how this can work.
So you have a bitmap constructor which takes a file name and then we're going to just emulate the fact
that we're loading the bitmaps So loading that map from.
And then let's put the file name in here and that's it.
So that's a diagnostic message for us.
Actually loading the image and then will implement the drawing function where we're just going to say
that we're drawing the map drawing that map.
Now we go.
OK.
So the problem with this bitmap is that we don't really need to load the image until the drawing code
fires.
So if nobody wants to draw the map there is really no point in us loading it from the file.
Now the problem with this scenario is that suppose that we don't have an option of going back into the
bitmap and modifying it let's suppose that it's once again this open closed principle or just bad luck.
Maybe we're just given this interface and we have to work with it so we can build on top of this is
we can build a virtual proxy which has the same interface as the bitmap meaning it has a constructor
which takes a filename as well as a method called drole but this lazy proxy that I'm going to build
is actually going to defer the loading of the original bitmap until somebody wants to draw it.
So here is how you would implement this so we would make a lazy app which is also an image.
OK so this time round we're going to do is when the constructor gets fired we're not going to actually
load the image from the bitmap.
We're going to just save the file name basically.
So here I'll have a private section where we'll have the file name itself file name like so and then
we'll initialize it in the constructor.
There we go.
OK so now that we have this constructor what we can do is we can define our operations so we want the
easy bit mapped to act in an identical way to an ordinary bitmap.
And so what we're going to do is we're going to aggregated but we're going to be really smart about
it.
So what I'm going to have is I'll have a bit pointer called GMP and it will initially be assigned no
PCR because initially we don't want the bitmaps constructor to fire.
So what I mean is that we're not going to say something like Oh let's initialize it here let's make
a new bitmap and then work with it because remember the bitmap constructor is the thing that loads the
stuff from the file name.
And we don't want it to happen until somebody actually goes and draws the whole thing.
So the constructor is going to be empty.
However what we're going to do is we're going to implement GROL and this is where we would check whether
the bitmap has been initialised and if not we initialize it then that's when the loading actually happens
so here we can say if not VMP then BNP equals a new bitmap with the file name because with cache the
file on them.
And then you can use this underlying bitmap to perform the actual drawing so instead of replicating
some drawing code here you just Coleby MP Draut.
So what we've built is we've built a kind of virtual proxy which allows us to get something which looks
like the ordinary bitmap but isn't.
And we can actually kind of simulate it's use so let's imagine that originally you have a bitmap which
looks like the following.
So you have something like this you have a map which looks like this.
And then what you end up doing is you end up actually just replacing the codes originally.
Let's suppose that you have VMP draw like this and you decide that you want to change the code to use
the virtual proxy All you have to do in this case is change from that map to lazy map and you've got
it basically so you've called the rendering now in a proper order which means that the file is going
to be loaded only when somebody actually calls the draw method.