Lecture thumbnail 0:00 / 0:00 So let’s get back to this idea of having this problem with specifying the right coordinates.
Now, so far, we’ve kind of gotten ourselves into a situation where if you invoke a constructor, that
constructor has too much going for it.
It has that extra argument and you want to avoid the argument.
So maybe you want to take the information about the coordinate system being used and put it in the constructor
name.
But of course you cannot change the constructor name because it matches points.
So what do you do?
Well, you can certainly go ahead and start using inheritance and inherit, let’s say, polar point
from Cartesian Point and all the rest of it.
But there is a much simpler way which is making a factory.
But before we get to factories proper, we have to discuss the first of the two factory design patterns.
There is actually two patterns being discussed here.
One of them is proper factories.
And in fact, we’re going to take a look at the abstract factory later on.
But the first pattern and the simpler pattern to understand is a factory method.
Now, let me actually get rid of everything related to polar coordinates for now.
So I will get back to having it like this without the switch statement.
So let’s get rid of everything and I’ll get rid of the argument and we’re going to rename the arguments
back.
I want them as X and Y, so I’m going to rename this back to X and rename the other argument back to
Y.
Okay.
Now, the reason I’m doing this is I want to show you something.
I want to show you a refactoring that’s provided by Resharper, which actually takes a constructor and
changes it into a factory method.
So bring up the refactoring menu and here we have an option of replacing the constructor with a factory
method.
That’s what we’re going to do.
So let’s invoke this and you get to make the factory method name.
So what do you want to happen here?
And this is precisely where the benefits of the factory method comes into play, because unlike a constructor
which is forced to have the same name as the containing class, the factory method name can be virtually
anything.
So for example, here I can say new Cartesian point.
There we go.
So I can press next here.
And a couple of changes have happened just now.
So first of all, you can see that the constructor we had is still here, but it’s private now, so
the constructor is no longer available to external use.
It suddenly available to internal use within the class, but it’s not available to anyone who is on
the outside.
And then of course, we have a new public static method called new Cartesian point, which returns a
point and it simply invokes the right constructor.
Now the key thing about this, and this is what’s called a sudden do this.
This is called a factory method design pattern.
The key point about this is that the name of the factory method is not directly tied to the name of
the containing class.
So here we’re making a new Cartesian point, but then of course we can employ polar coordinates so I
can say public static point, new polar point.
And here I can specify different argument names.
So I can have two double arguments just like here, but I can call them rho and theta.
And this lets the API user understand what’s going on, which of the arguments goes where.
And then of course we return new point where we have rho times cos theta and rho times sine theta and
that’s it.
And the way that you actually work with points from now on is as follows.
So if you want to make a point, you just say point dot new polar point, for example, you specify
the coordinate in terms of the radius.
So 1.0, for example, and then the angle.
So you can say pi over two, for example.
And if we actually output this point, so let’s actually write line the whole thing and I’m going to
generate a couple of members here.
So I’ll generate the formatting members for X and Y.
Let’s actually run this and just see what we’re getting.
Now, these are probably not the values you expect, considering that I was putting Pi over two here.
But the reason is that we are working with degrees instead of radians.
So if you correct for those we would get the right answer.
But anyways, this is how you make a factory method.
So the advantage of a factory method is twofold.
First of all, you get to have an overload with the same sets of arguments.
So we have two doubles here and two doubles here, but they have different descriptive names.
So the API tells you what arguments you’re providing.
And second, the names of the factory methods are also unique in the sense that here, right in the
name, we’re making a suggestion as to what kind of point you are creating.
So once again, this is an API improvement.
Play Play Stop Play Play Play Play Play Play Start Play