There is one rather critical thing that's missing from factory implementation and that is the user being
able to understand them and to use a factory.
So at the moment there is no direct link no obvious link between point factory and point.
So if somebody comes up to let's say they come up to the Point class and they see for example that everything
is private so let's suppose that we just get rid of this everything is private.
They see that there is no API for them to call except maybe I don't know maybe this stream output operator
or something they might be particularly upset about this because it's not clear how you would actually
go about using the point.
So in this case what happens is people actually sometimes put the factory itself right inside the type
and expose it as a way of giving a hint as to what you should be doing what you should be constructing.
And this also solves a second problem which is the problem of breaking the open closed principle.
So if you're prepared to sort of plan ahead and make sure that your factories is made the same time
that your actual type is made you can avoid this whole business of having the friend class in the first
place because in her classes if I take this point factory and I cut it and paste it inside point in-in
classes have access to the private members of the outer class so here we can use the constructor of
point even though it's private.
So this solves a particular problem but now the question is how do you want to expose the point factory.
So at the moment obviously you've broken this kind of code.
But what we can do is we can go through the factory and get the factory through the point.
So instead of saying point factory.
Colin Colin you Polar you would say point.
Colin Colan point factory Colin calling you polar.
So the reason why this is better.
The reason why this is somewhat better is if somebody comes up to a type Kolta point and they see that
most of the members are private Well they're going to see is a static member or in a class called Point
factory.
Well we haven't made a static member yet but the inner class they're going to see the inner class and
so cold the inner class and you can call point point factory new poler and they get to see what the
API is so it's somewhat more intuitive for them to kind of figure out what's going on.
Now if you wanted to what you could try to do is hide it even more in the sense that for example you
could make the point factory private.
Now we have a problem here because now you can no longer do it like this but what you can do is you
can make it effectively a singleton.
So we're going to look at the singleton pattern in a while but essentially what you can do is you can
make an instance of the factory and expose that instance directly from the point.
So here what I can do for example is I can say well let's have a static point factory called factory.
Now this is interesting because what happens now is if you take a point of view just take a point and
do colon colon on it.
The only thing you're going to see like the only member is a factory and so you make you get this point
factory and then you put a dot here and you have your options for creating whether you want to Cartesian
or polar point.
So once again this is yet another alternative.
And typically what this approach to the pattern is called is an inner factory because essentially the
point factory is an inner class right inside the point class and it's really up to you whether you want
this class to be private or public whether you want it its members to be static or non-static it doesn't
really matter whether you want to expose it as a static variable or maybe you want to have some sort
of gets factory method here it's really up to you.
So there are lots of variations.
But the upside of the inner class is first of all it has access to the private members so here we're
calling a private constructor without any problems.
And also you get some control over its lifetime.
So for example if you want to hide the factory and prevent anyone from actually making an instance of
the factory you have to do is make the constructor private and then expose it as a field or some sort
of get method.
For example.