Lecture thumbnail 0:00 / 0:00 One of the reasons why factories exist is because constructors are actually not that good.
And we’re going to see what the problem with constructors is.
So let me give you a very simple example.
Let’s suppose I’m making a class which represents a point.
So we have the X and Y coordinates of a point.
We may as well have them as fields, so we’ll have double X comma Y.
That’s going to be the coordinates of a particular point.
And then of course, you go ahead and you make your good old fashioned constructor where you initialize
the X and the Y.
Now what happens later on is you realize you also need to initialize the point from polar coordinates
and this becomes a bit of a problem because now one of the things you cannot do is you cannot really
overload the constructor with the same exact argument types.
So I cannot make a point which takes the polar coordinates.
I cannot take rho and theta because as you can see, the signature is exactly the same as the point
above.
So it’s just the names which are different and the names are not in any way a way of figuring out dispatch.
They they’re not a way of figuring out which of the constructors to call.
So we cannot do this.
So the question is, well, given that we want to use constructors, how can we actually get ourselves
in a situation where we can initialize a point from both Cartesian coordinates as we’re doing here,
as well as polar coordinates?
And the answer is that you start introducing other unpleasant types, Like for example, you start to
introduce a coordinate type of some kind so that you can pass it into the constructor.
So for example, you can say, Let’s have an enum called Coordinate System and you have Cartesian,
Cartesian, and then you have polar.
So what you end up doing then is you end up adding an additional argument to the constructor which specifies
the coordinate system and you may as well give it a default value of some kind.
So we may say that it’s Cartesian by default unless somebody wants to have it in Polar.
Now we already have a couple of problems here because you can no longer have the arguments as X and
Y.
Remember, x and Y kind of imply a Cartesian coordinate system, but we might be working in polar,
so you cannot do rho and theta either.
You have to make them as neutral as possible, so you may as well make them A and B and something.
So this is how you would go about it.
And then of course, what would happen in here typically is you would do a switch, so you would switch
on the coordinate system like so and let’s generate the switch labels here.
And in the case of the coordinate system, in the case of Cartesian, in fact everything is easy.
You just say X equals a, Y equals B, Although notice that when we have these values, there is no
indication that X is here and Y is here because their names are A and B, which can be anything.
Maybe we’re writing B to X and A to Y, who knows?
So that’s one point of confusion.
And then of course, for the polar coordinates, you say X equals a times math dot cause of B, and
then Y equals a times math dot sine of B, something to that effect.
So once again, there is no indication here that this is a row and this is theta and you would have
to add XML dot comments in order to actually let people understand how you’re using A and B to initialize
a point from a particular coordinate system.
And in addition, what you’ve done is you’ve required now everyone to basically specify the coordinate
system unless they’re working with Cartesian.
So that’s very inconvenient.
So you’re still tied to the name point because the constructor name has to match the class name, but
you don’t have any flexibility in terms of actually communicating the information to the user.
So your summary has to say initializes a point from either Cartesian or polar.
And then for a you have to say something like X, if Cartesian Rho, if polar, and you then better
hope that people actually read these comments because if they don’t, they might misconstrue it as something
else.
So this is the kind of problem, this is the precise problem, which factor is actually solved for us.
So that’s what we’re going to take a look at next.
Play Stop Play Play Play Play Play Play Play Start Play information alert