Lecture thumbnail 0:00 / 0:00 So one side effect of using the adapter is that you generate a lot of temporary information.

And in our case, what’s happening is as we invoke the draw method more than once, we’re actually regenerating

some of the information that’s already been calculated.

So as you can see here, for the first eight invocations, we’re generating points for the lines which

make up the two rectangles.

We have two rectangles.

Each one has four lines in it.

So there is a total of eight invocations.

But then when we call draw again, we end up having to do the same work all over again and recalculating

those points, which isn’t very efficient.

And we might want to avoid this.

And for this reason you use a very well known mechanism called caching, which means you simply preserve

the information that you stored for future use.

So let me show you how this can work in our line to point adapter.

What we’re going to do is we’re going to add a static member of type dictionary dictionary like so,

and it’s going to go from int to a list of points.

So this is going to be our cache.

So let’s initialize it new dictionary of int and list of point Resharper.

Failing me again here.

Okay, so this is our dictionary.

Now the question is what is the key?

What’s going to be the key of these points?

And the key to these points is going to be a hash code of the line.

Now, we want to have a way of uniquely identifying a line and calculating its hash code is very easy

because a line is composed of points and points are composed of X and Y coordinates, which are all

unique, which means you’re unlikely to get hash collisions on identical points, identical lines.

So let’s actually generate gethashcode first of all.

So let’s go into the generate menu and we’re going to generate equality members.

So if you want to get hash code, you need equality members.

And here you specify all the members which are used for equality as well as obviously the get hash code.

I don’t really care about the equality operators themselves.

I just care about get hash codes.

So I’ll press finish here and this is the only thing I care about.

I may as well get rid of everything else, but let’s just keep it there because we got it for free.

And the same goes for lines.

So here with line, we once again generate code, generate the equality members and once again, I’ll

just pick, start and end and we are essentially done.

So if you look down below, you can see that we’ve got get hash code for the entire line and that’s

what we’re going to be using as the key to our cache.

This integer here is going to be the hash code.

So the idea is as follows Before you actually generate the points for a line, you check whether or

not you’ve already got it.

So you get the hash code for the line.

So you say line get hash code.

And if you’ve already got the hash code in the cache, then there is nothing that needs to be done.

So if cache contains key hash should have been an H in here.

H in here like so.

Then what you do is you return.

We don’t generate the points once again because we already have them.

So there is no problem here.

And then of course we continue with the generation, but we do it differently.

So we make a list here, list of point.

Let’s just say var points equals new list of point, and then instead of adding the elements to ourselves,

we’re actually not going to be a collection anymore.

So instead of adding the points to ourselves, what happens is we call this add on the set of points.

So we say points dot add.

Points add and the same goes here, points add here.

And then, of course, what we do is we take the hash code and the points and we add them to the dictionary.

So we say cache dot add and we add the hash as well as the points.

But now, of course, what happens is we are no longer a collection or we are a collection, but we

are not storing any elements, so we may as well get rid of this.

Now what we need to do is we need to expose all the points inside the actual dictionary.

So we have to make ourselves a enumerable, so we become an enumerable of point and we implement the

missing members.

And of course, we already have a dictionary where all of the points are actually kept.

So all we have to do is we have to say, please give us all the values in the cache.

So we return, we take the cache values.

We do select many to flatten the lists into a single list.

X goes to X, And then what we do is we say get enumerator, and that’s how we can return all of the

points.

So now if we execute this and by the way, note that we don’t have to rewrite any other line of code

apart from this.

But now if we execute this, you’re going to see that we’re getting the same results.

But something interesting has changed.

Instead of calling the generation 16 times, you can see that we already have eight invocations only

and the rest of the invocations are unnecessary because we’ve already generated the points for those

lines.

So instead of getting them from the dictionary, what happens is we already have them, we’ve already

generated them, so we avoid generating them once again.

So this is how you construct an adapter for the interface that you have using caching this time around.

So this time around we’re not just blindly converting it on each constructor call.

In our case, we keep the static member, which actually keeps a cache of all the values that have been

generated for the particular lines.

And suddenly using Gethashcode is one alternative to how you can make sure that you check rather how

you can check that you’ve already generated the appropriate constructs and you don’t need to regenerate

them again.

Play Play Stop Play Play Start Play