0:00 / 0:00 Remember at the very start of this course, we talked about separation of concerns and the single responsibility
principle.
Well, it can be argued that the construction of an object is a separate responsibility from what the
object actually does.
So certainly in the case of simple small objects, having a constructor makes sense.
But if you do require a factory, then why not take that factory into a separate class?
So instead of having a bunch of factory methods right inside point, how about we make a point factory
and just have the methods there?
So let’s try doing exactly that.
So what I’m going to do is I’m just going to make a class called Point Factory like so.
And then what I’m going to try and do is I’m going to try moving these two factory methods right inside
Point Factory.
So I’m going to go into move to another type, and here I’m going to type in point factory.
There we go.
So that’s what we’re going to have.
I’ll take the new Cartesian point as well and try to move both of them there.
So let’s press next.
And now we have a bunch of problems because, of course, we made the constructor private.
So this is one of the challenges which are particular to the C sharp programming language.
If you want to have an external component, you cannot just access some private members.
So one of the kind of obvious solutions here is to just make the constructor public, and this certainly
solves the problem.
However, you might be adverse to this kind of approach and we’ll talk about how to mitigate it later.
But for now, we’re just making things public, and as a consequence, we now have a point factory,
which well, you can see the complaint here that it’s never instantiated.
But the point is it’s composed mainly of static methods, so it may as well be static.
It’s really up to you whether to make it static or not.
It doesn’t affect things that much.
But now what you can do and you can already see the refactored code here is you can make a point by
calling another component.
So instead of calling on point, you say point factory dot new polar point.
Now, the only consequence of what we did is that we haven’t really hidden the constructor and as a
result, people can still do things like var P equals new point and then they can provide the X and
Y arguments.
But once again, we’re keeping the arguments nice and clear so it’s x and y, not rho and theta.
But if you want to initialize a point with polar coordinates by using the constructor, well you can’t
because we don’t have such a constructor.
We have a constructor for X and y only.
Okay.
So this is what’s called a factory.
So this is the kind of classic example of a factory.
So a factory is just a separate component which knows how to initialize types in a particular way.
And the caveat, so to speak, is that the constructors have to be public.
That’s a kind of obvious constraint, but that’s a constraint we’re going to mitigate in just a moment.
Play Play Play Play Stop Play Play Play Play Start Play