Lecture thumbnail 0:00 / 5:24 So in this section of the course, we’re going to talk about the flyweight design pattern.
And the flyweight design pattern helps us accomplish a single goal.
And that goal is space optimization.
And by space, we obviously mean the amount of memory that our programs actually take up.
So what is the motivation for using a flyweight?
Well, the goal of the flyweight is to avoid redundancy when storing data.
So, for example, if you think about a typical massively multiplayer online RPG, you’re going to have
plenty of users with identical first and last names.
You’re going to have lots of people called John Smith, for example, and there is absolutely no point
in actually storing the same first and last name data over and over again.
This applies particularly to, let’s say, a database that you have on disk.
So typically in a database you would have a database field for the first name and a database field for
the last name.
Well, it’s very inefficient if you’re going to have 100 people called John Smith, but you’re also
going to have people called John with a different name or having the last name Smith, but having the
same or different first name.
And you’re going to have lots of duplication if you just put in the data over and over again.
So what you really want to do is you want to store a list of names somewhere else.
You want to store a list of names in a separate table, maybe a separate store, and then simply have
the pointers to those addresses.
And by pointers, I mean it can be references, it can be indices which actually point to some sort
of common store.
So you want to avoid all of this duplication.
And that way you have just one entry for John Smith, for example, and maybe the name John has index
five and the name Smith has Index ten.
And that way if you’re referring to somebody, you’re not referring to them by the full string, but
you’re saying, Oh, his first name has index three or index five, and his last name has index ten,
for example.
That way you are saving a ton of memory and this can be important both in terms of the random access
memory as you’re working with an application and certainly in the case where you’re working with databases,
because if you have millions of players, for example, where there is just no point in keeping so much
data.
So one of the things that you probably know about Dotnet already is that the net framework actually
tries to do something very similar inside of itself, something called string interning.
So essentially the idea is that in Dotnet strings are immutable.
You cannot take a string and change it.
You can make a new string from an existing string.
Or if you do need something that is mutable, if you need a string which can actually be modified on
this sort of character level, then you’re going to be working with something like a stringbuilder or
you’re going to be basically working with an array of characters or something to that effect.
So the net framework, what it does with strings, because they are immutable, is it performs a string
interning, which means that if you have more than one string, which each contains the same text,
they’re actually going to point to the same region of memory.
And that is once again a memory saving technique.
So what I’m saying here is that the the pattern, the flyweight design pattern that we are talking about
here is actually built in right into the net framework.
And you don’t even have to think about it.
In fact, you have no control over it to an extent in the sense that you simply can determine whether
a string was interned or not.
But that is something that the net framework does for you, and it works rather well.
So another example is let’s suppose you’re outputting text to the console, you are writing text, and
some of the text might not be ordinary text, it might be bold or italic text.
But if you’re going to output text to the console, you don’t want each of the character to have yet
another formatting character.
You don’t want to double the amount of information that you need to provide in order to format text
as you print it out.
So instead of operating on individual characters, you take a wider look and you start operating on
ranges, for example.
So you can, for example, specify that all of my text is plain text.
It should be written to the console as is, except for this particular word.
And if you want to describe a word as a chunk of the overall text, you would describe it in terms of
a range.
A range typically being the, let’s say the line number.
If you want to format the whole line or you can say, I want to start at character 34 and finish at
character 77, and all the characters in between have to be formatted in a different way.
And this is once again an application of the flyweight design pattern.
So the flyweight is quite simply a space optimization technique that lets us use less memory by storing
externally the data associated with similar objects.
And the key here is that it tries to avoid duplication.
It tries to avoid spawning too much data.
Keeping too much data.
Instead, we adopt a kind of database reduction approach or a database optimization approach in that
instead of having lots of associated objects, you just have references to other tables or other stores
where this kind of data is kept.
And we’re going to look at how exactly all of this implemented in just a moment.
Play Play Play Start Play information alert