Lecture thumbnail 0:00 / 0:00 So one location where the strategy pattern is used quite a lot is when it comes to equality and comparison
in the net framework.
So let me show you a very simple example.
Let’s suppose that you have a class called person.
So you have a class called person.
Let’s suppose that there’s just a bunch of fields.
So we have maybe some identifier, like you’d have a primary key in a database.
You’d also have a person’s name and you could also define, for example, a person’s age like so.
So we can make a constructor which initializes all of these.
And let’s imagine that we have a list of people.
So we have var people equals new list of person.
Let’s suppose that we got the data from a database and now we want to, for example, sort this list
so I can say people, people dot sort and what do you think would happen here right out of the box?
The answer is nothing.
The answer is that here when we do the kind of sorting without specifying what we’re sorting by, we’re
not really going to get any results.
We’re going to get the default array dot sort implementation because that’s what a list will actually
do behind the scenes.
It will go to array sort.
So we’re not going to get anything because at the moment the dot net framework doesn’t really know how
to sort a person object.
We have to give it a strategy for how to do that.
Now there are two ways of doing it.
You can specify the default strategy for all types, all, all instances of class person.
You can specify the default strategy or you can specify custom strategies.
So if you want the default strategy, just go and use your ID, whatever ID you use, And what you want
to generate is you want to generate relational members.
So in this case, I want to use the ID for the actual implementation.
So I want to use the ID field and after I generate code, you can see that I get like lots of stuff.
So suddenly my person class becomes an icomparable of person.
It also becomes just ordinary icomparable the kind of weakly typed version and then I get lots of generate
code for first of all, compare to.
So remember, compare to is something that is basically going to return an ordering relation.
So it compares the IDs because that’s what I specified.
So it does id compare to other.id and it’s going to return negative one zero or positive one depending
on the ordering relationship between the ID of the current element and the ID of the other element we’re
comparing to.
So if it gets minus one, that means the first is less than the second.
If it gets a zero, that means they’re equal.
If it gets a one, that means that the second is greater than the first.
So then we have the weakly typed version.
You can see that there’s plenty of scaffolding here.
There’s also type checks because it’s weakly typed.
You have to basically cast it to the right thing and then you sort of you throw an exception.
If that doesn’t, that doesn’t match what you were looking for in the first place.
And then you have the relational operators like less than greater than less than or equal, greater
than or equal.
These are all automatically generated for you.
So as soon as we’ve done this, people dot sort now takes a meaning.
When you say people dot sort, you are now sorting by ID.
Of course what you can do is you can be explicit about it.
You can say that, well, we are in fact sorting by ID and you can sort by other criteria as well.
So for example, I can say people dot sort and here I have lots of options of what to specify.
So for example, I can specify an I comparator, which is just a delegate.
So here I can specify a lambda which takes two people X and Y, and it basically returns the comparison
of their name.
So I can say x dot name and then compare to y dot name.
That way we’re actually going to sort by name.
As you can see, it’s a rather long winded kind of thing.
Typically you’d use Linq and in Linq you would just say get use the name for for actual sorting.
So this is somewhat long winded.
This is not the Linq sort.
This is the sort that’s built into the list type.
But what about strategies?
What about the different strategies that I use?
So here this is kind of the default strategy.
The default strategy is just to compare objects using relational operators, and we’ve defined lots
of them up here.
We’ve defined all of these things, we’ve defined compare two.
And so now this this approach people dot sort is going to use the ID by default here.
We’re specifying the strategy as a lambda.
We’re basically saying this is the strategy that we want to use in the sorting process.
What you can also do is you can use the cogeneration facilities of whatever you’re using to generate
additional compares.
So here we’ve made a default comparator which compares by ID, but we can make additional compares which
compare by name, for example.
So here once again, sort of behind the scenes, I’m using the code generation facilities for a relational
comparator.
I’m specifying the name as a compare and here I get some generated.
So here you can see that we get plenty more generated code.
So we generate a sealed class called name Relational Compare.
And all it does is it implements an icomparer of person and then you have a public int compare method
which compares two people X and Y, as you can see here.
And then it goes through the motions comparing the actual strings, kind of like what I’m doing down
here, except that I’m not using the ordinal comparison.
So here I’m using compare two, which is actually culture specific.
So that can backfire on you.
And down here what you can see is we also have a static member which exposes the compare as a static
member.
This is just a usability thing, basically.
So what you can now write is instead of writing this, what you can say is you can say people dot sort
person, dot name compare.
And this is once again going to sort by name by comparing people’s names.
So in all of these cases you are using strategies.
So in people sort you’re using kind of the default strategy, the one that’s provided automatically
either in the class or if you forgot to actually generate it, it’s just going to use the default approach,
which isn’t really useful because it’s probably going to look at memory addresses of objects or something
because there is no real information that can be used for for the purposes of sorting.
So the second one is where the strategy is provided by a lambda, and here we’ve specified the lambda
to be used for comparison.
This is actually if you’re thinking about Linq operators, then every single Linq operator is effectively
fed a strategy that it subsequently adapts to finding, for example, unique objects by a certain field
and so on.
And finally, what we’ve done here is a bit of a utility.
So here what we’ve done is we’ve generated additional inner classes.
So here is the name Relational Compare class.
So we’ve generated an additional class which is subsequently exposed as a static member.
And this is once again a strategy.
So it doesn’t have the word strategy in its name.
But, you know, it’s a strategy.
It’s a strategy for comparing objects, using the name field as the discriminator, as the part that
you’re actually comparing.
So this is how the.
Net framework uses the strategy pattern to perform comparisons.
And of course, the same goes for equality.
So here we’ve talked about things like Icomparable, but here similarly you would have I equatable and
then you would have equality strategies when looking for distinct objects.
For example, looking for objects with unique names or unique IDs.
For example, you would have a an equatable and you would use that to compare the objects.
So that’s how that framework actually uses the strategy pattern.
Play Play Play Stop Start Play information alert