So in our discussion of the prototype design pattern and we basically had a variable called Jaun which
was the perfect type and that's the variable that we actually replicated when we wanted a copy of it.
Now this isn't particularly convenient to the consumers of your API because you might want to give them
a prototype to work with.
So the question is Well how do you give them a prototype of anything.
So one idea would be for example to make just a global variables so if you want someone to consume a
particular prototype you could just make a contact contact which would be a global variable.
And in this case you could for example just just replicate this contact here just replicate this Contac
then you could call it for example main If this is the main office.
So this is your main company office.
You can leave the name as well as this week number as just the faults for example having a 0 and an
empty string and you might expect people to actually replicate this by using either the copy constructor
which we've implemented.
Or you could do a copy assignment operator very similar thing.
So whichever idea you go for this isn't still Nahda not explicit enough in terms of letting people know
that you actually allow them to make these copies and another question is What if you want to constrain
people so they only create instances from a prototype and so that they cannot make individual instances
by themselves.
So maybe you don't want to expose the constructor.
And in this case what you would build if course is a prototype factory.
Now I'm not going to go through the trouble of making these constructors private but you can imagine
them being less than public and having people only go through the factory.
So what we're going to do is we're going to make an employee factory.
So you have a struct employee factory and this is the factory which can actually produce employees in
either the main office or the auxiliary office for example.
So what you would do here is you would have maybe a contact definitions for both the main and auxiliary
offices.
You would initialise them somewhere of course and then you would expose them actually we don't even
have to have them here because we can have them as the individual members of whatever factory method
we actually decide.
So let's imagine that somewhere in the depths of the employee factory we actually have a very general
function for making a new employee.
So let's suppose that we have a static function which returns a unique PVR.
So let's suppose we decide to use smart pointers so you return a unique PDR contact and there's going
to be called a new employee.
So you would provide the name as well as the Swede and of course you would also provide the prototype
to build from.
So I would call this contact Kroto are actually.
Let's let's let's have the full name prototype because we are discussing the prototype design pattern.
So you provide the prototype as well as the pieces of customization and what happens is you can you
copy the prototype effectively and then you do the customization.
So you say auto result equals make unique contact from the prototype.
So notice once again this is where you pass in the prototype So you're expecting a constructor which
takes another contact.
So you're expecting a constructor which looks exactly like this.
So we make a copy of whatever it is that we're constructing then we customize it.
So for example we say result name equals name and result result address.
Sweet sweet.
And then we return the result.
That's pretty much all that we need to do here.
And now of course we can actually use this utility function inside our factory so if you want to make
a main office employee for example.
So if you want a static unique contact new main office employee so all you have to provide once again
is there a name and where they work so called String name.
And INS was half constant.
Sweet like so and I may as well add a constant here just for the fun of it.
OK.
So what you would do here is you would use the utility function.
But before you do that you would have a prototype somewhere.
So here for example you can make a static contact which is the prototype I'm going to call it P and
you're going to have the empty name as well as a new address where you manufacture the actual 123.
He's dry for example in London and the suite is 0.
Once again I'm forgetting the quotes here.
So once you have this what you can do is and this is a static variable by the way that you can use.
So when you return something you return a call from this utility function which is called new employee.
So you return your employee and you pass in the name of the suite as well.
Has the prototype which in this case it's called B and you would do the same if you have an office for
example.
So what this gives us is an ability to now go into contact and maybe hide away some of the constructors
if you want to only use the factory but it doesn't really matter.
In either case we have this factory to work with.
So what we can do is we can sort of come in here and what we can construct is well first of all we can
construct the actual factory so we can say well let's have one of these employee factories employee
factory.
But in actual fact the member that we need is static so we can just say new main office employee we'll
call him John and he's going to be in suite 123 and I can just say audit John.
He quolls and then do this vocation of course it's now a unique BPR because that's the way that we've
set this up.
So if I wanted to print.
John I have to put a star here in front to actually follow this smart pointer and get the actual info.
So now we can just try executing it just to see that it does in fact work which I hope it does and hopefully
we get Jones particularise And here is what we get so we have the name Joe on the street is 123rd drive
in London and so is 1:23.
That's precisely what we've defined here and here as well as in the prototype.
So essentially if you want to keep your prototypes in a safe place then you can stick them into a factory
and then get the people to use the factory maybe use it instead of the actual constructor's or maybe
use them alongside the constructors to actually construct objects from the prototypes which in our case
we simply declared static so we have the static prototype and we can keep reusing this prototype to
make new instances with a predefined address.