All right.
So we have two factory methods inside point but sometimes what you really want to do is you want to
do even more in terms of separation of concerns.
So if you have dedicated code which is dedicated to the construction of the Point object why not move
it into a separate component.
Why not move it into a separate class for example.
So that's exactly what we're going to do.
So we're going to make a structure called Point factory.
So what this actually is is a dedicated factory now in the Gang of Four There is no pattern called factory.
There is the factory method and the abstract factory.
Well this is going to be a concrete factory.
Definitely not abstract.
So what's going to happen here is of course we're going to take these static methods for new Cartesian
and new poler.
We're going to cut them from the time itself and we're going to stick them into the point factory instead.
So we're going to sort of put them in here put them into point factory and then there are lots of different
options as to how you want to orchestrate it how you want to actually expose the point factory because
one of the options of course is for people to actually construct the factory itself.
So somebody can for example you can suddenly you're not prohibited from making a point factory for example
and then you can call something on.
However since we made the members static and I have to remind you that this is actually optional you
don't have to make them static.
They can be static.
They don't have to be static.
You can actually have it both ways.
And so if you want to avoid for example constructing the point factory itself you can just say foin
factory point factory coal and coal and you Pooler that's going to work.
The only problem right now as you can see here is that the private constructor is inaccessible.
So this actually requires a rather unpleasant change because you have to have a friend class Point factory.
And unfortunately as you have probably guessed this violates the open closed principle because essentially
the French class mechanism is very hostile to the open closed principle.
You see what we've just done is we went into a class that we've already written and we added something
to it.
We had this friend class declaration.
Now the reason why it's bad is because if you imagine somebody makes a point and there is no need for
a factory so you go on for a whole year programming your code without the need for a factory and then
you come back and you decide you do need the factory so you will end up jumping into a class Point and
adding that French class declaration.
So that's the that's where the open closed principle is actually broken but provided you're OK with
it what you can do is you can actually access these private constructors of course.
One thing you can do as an alternative.
You don't want to break the open closed principle is simply make everything public simply make the whole
thing public and this way you have your constructor being available to whoever is actually interested
in using it.
And as a result a point factory can co-exist without any modifications to existing code so if you make
everything public then that's OK.
That's you know completely fine in terms of the open closed principle.
You do expose the constructor but this constructor is very sensible it takes the X and the Y.
So the user now has two different ways of constructing a point so they can use the point factory like
boying factory colon colon you poler or alternatively they can make a point straight away they can say
or P2 equals.
Well actually they can just write Point two with coordinates 3 and 4 and these would be Cartesian coordinates.
Now we're not being explicit in the constructor that this is a constructor which takes Cartesian coordinates
but hopefully the x and y are sufficient hints to the consumer of the API that we are in fact taking
the x and y coordinates.
So this is how you implemented a typical factory.
So just in ordinary class which contains a couple of methods for the construction of something else
and it has to be a friend class if you want to access private members but you can just make everything
public.
And this way you don't break the open closed principle.