All right so let's first of all try to recap the previous example that we looked at.
So we essentially had an interface for drawing points but we only had lines so we had to build this
line too pointed at her.
Now we look at the adapter itself it constructs a bunch of points from a lie.
And unfortunately as we resize the window we found that it would construct this set of points again
and again and again and this might not be necessarily what you want to have you might want to have the
set of points constructed once and then being reused as the window is resized and so on.
So that's exactly what we're going to do here we're going to build a caching adapter.
So the caching adapter is an operator which also generates some temporary information but it doesn't
regenerate it on every single step.
Now there are a couple of steps that are required to be performed before we are actually able to do
that.
And one of those steps is to figure out how exactly can we cache these different objects because essentially
one of the problems is how do you compare a point with another point how would you compare line with
the line.
Now there are different ways of doing it you can certainly just do value comparisons here.
But one of the ways that this can be done is by genuinely hash codes and this is a more general case
if you want objects to be kind of generally comparable in their very fast kind of way.
You can generate cash goes for the different objects and in actual fact we're going to be using the
ID here to help us do that.
So in my case what I'm going to be using is I'm going to be using the boost functional hash header to
generate the actual hashes for the point so if I want to get a hash value for a line and the line is
composed of points I obviously need the hash values for the points as well.
So typically what you do is you go into the cogeneration menu in whatever ID you have.
And in this case you would go for a hash function.
All right so you can generate the hash function for.
You can specify the members for which a hash function is actually generated.
And here you can either use the boost variety or ordinary shift in X or so I'm going to go with boost
here and just press finish and here is my hash function so I have a hash value which just takes some
seed and then uses boost hash combine to incorporate the information about both of the fields into the
calculated hash.
So we can do the same thing for the line as well.
I can just go in here and also generate the hash function in a similar fashion.
And then we can continue and actually using it.
Now as I'm sure you've guessed we're going to build a new adapter so we are the have the line 2.0 point
adapter.
So we're going to build that line to put in caching adapter in this case and what we're going to do
is we're just going to cache the information about the hash codes for all the different lines so that
we don't replicate them unnecessarily.
So let me show you what this can look like.
Now I'm going to let me just bring up the bring up the preview on the different screen where I'm cheating
by looking at the complete results and then we can go and actually build this caching adapter so it's
going to be slightly different.
So we're going to have a line to point caching and after that we go.
So once again our typedef vector all point to be points like.
So we'll have a constructor.
I wish it would generate it like this.
Let's just see Tor.
Hopefully that generates just an empty constructor for us and then I'm going to add the line here once
again as a reference for which we are generating the points.
So now the things are a bit more interesting shall we say because first of all before we generate the
points for line we actually check the hash and then of course the storage changes so we're going to
have a different kind of story.
So I'm going to have a private section here and while that's on indented like so and in the private
section I'm going to have a map so I'm going to have a static map going from size underscore T which
is the hash value to the set of points which have been generated for the line with this particular hash
and I'm going to call it cash like so also keep the hash value of the last line that was generated.
So I'm going to have size on this courtsey line hash that's going to be a non-static hash of this particular
adapter's line because it's going to be used for generating the hash code for a particular line and
that's precisely what's going to be happening in the constructor.
So here in the constructor what we're going to say is we're going to say that first of all we generate
the hash and we'll check whether the hash is in the map.
If it's already in the map there's no point in actually generating anything so I'm going to do a hash
for a line kind of called hash and then I'm going to just say a line hash equals hash of line.
And that way I get my hash value and if I find it in the cache.
So if I say cache find like hash and if it's not equal to cache dot and then what I can do is I can
just return because there's nothing for us to do.
We already generate that information for this particular item.
Otherwise what we do is we perform pretty much the same operations as before except that we also incorporate
them into the cache so we're going to look something like the following.
So here we perform the same manipulation and we stick it into the cash so we get the points and we put
them into the map with the appropriate line hash.
So whenever somebody asks for it again it would already be there.
So this is a significant saving in terms of the effort and we can go down here to where we are rendering
things and we can change this from line to point to Napster to a line to point caching and after.
OK.
So this is pretty much all that we need to do.
And of course there are also these begin and end implementations so let's go and fix those.
So line two point adapter I'm going to just take the.
Well here we have the begin and the end in terms of a vector because that's exactly what we have.
But if you have a caching adapter Well you have to get the values for a particular hash value and this
is why we kind of preserve it here.
So here we have the line hash that we've generated from this particular adapter and what we can do is
we can reuse it to actually return the begin and end values for from the map effectively.
So this is how the whole thing is implemented.
So coming down here we no longer have any issues we can now save this and actually try running this
application.
All right so here we go here is the application itself I mean bring it onto the screen now the critical
part is what happens when we resize it.
So as I resize the application I try to it's not easy here with these Then borders as I resize the application
you'll see that we're not getting any additional output in the trace window and that's because we are
caching the values so there is no point in actually regenerating the values considering that we already
have them.
So the takeaway from this is that the adapter design pattern sometimes spawns temporary objects and
it's a good idea to cache them if you can so that you don't end up doing extra work.
That's not even required for anything.