The flyway design pattern isn't exactly new.
And this approach of caching information is something that people have already packaged into different
libraries for you to use so instead of building all these wonderful by maps and whatnot what you can
do is just use a library solution.
Now in my example I'm going to use the boost flyway implementation which basically just gives you a
type goes flyweight which acts as a flyweights Surprise surprise.
So let me show you how this can work when we construct whole user to.
Now in this structure also going to have first name and last name o define them as flyweight offspring.
So first name and last name.
Now what we can do is we can certainly generate a constructor for this so I can go ahead here and I
can make a constructor which actually initializes first and last name of course I want to initialize
them from strings not from actual flyweights.
So here I'll get rid of this part and just leave the strings as they are.
But apart from that the assignment itself is actually completely valid.
You can assign a flyweight of string from string and similarly you can take a flyweight and for example
printed to the command line without any particular issues.
OK so having made this.
And let me just beautify it a little bit.
What we can do is we can construct these uses so I can say use it to.
And let's call it first John Smith and let's have a second user or James.
OK so we've got these flyweights but for all intents and purposes they behave as just ordinary string
So for example I can see how user our first user Daut first name.
For example I can see something like this it's not going to be a problem we can actually execute this
and we're going to get a string not some you know bizarre representation of the wrap her or anything
like that.
Well we'll get the actual underlying object which is convenient shall we say because it looks like you
know something is wrapping the string here.
But we're going to be just fine.
So here's the output we get to see John.
OK.
So let's actually verify that the name Smith is not replicated and the name John and Jane are in fact
different.
So for this what we're going to do is we're going to basically get the actual pointers to those strings
and we're going to compare them.
So I will see our bull alpha so that we get to see booleans as true or false and then I'm going to see
the following.
So first of all let's take a pointer to the first uses first name.
So first up first name doth gueth so good gives us the actual object and then we can get a pointer to
it by just using the ampersand.
I'm going to compare it to a pointer to the second or first name and don't get so we're expecting a
false here but if I do this for the last name because remember they are both.
They both have last name Smith.
We should get P true here because they are identical so let's go and put in a last name in here and
let's see what we actually get as we compile and run this.
All right so as you can see we get a false in the first case because essentially these uses are called
John and Jane So these are two separate objects they're not identical but when we get the last names
which is Smith in both cases we'll get a value of true which means that their last names when you actually
get them and get pointers to them they actually point to the same object which is what we want from
the flyweights in the first place.