Before we go ahead and actually implement the different factory design patterns that we're going to
be discussing I'm going to give you an example of where the factory design pattern is actually relevant.
So let's imagine that we're building a structure for representing a two dimensional point.
So I have a point and I have the x and y coordinates perhaps so x and y.
Let's suppose I want to have a constructor.
So I'm going to put a constructor in here that initializes x and y.
So far so good.
But this point is storing its information in Cartesian coordinates and then suppose I want to initialize
it from polar coordinates instead.
So what would I do now.
Typically the first inclination of mine is to make a constructor call point which takes a row and theta
and simply assigns the x and y to use depending on the row and the theta.
Unfortunately we cannot have two constructors with the same set of arguments to floating point values
here are two floating point values here.
That's simply not allowed.
So we also are mandated to have the name of the constructor.
Match the name of the structure or in the class that we're creating So we cannot rename it to a let's
say a point from poler or something like that.
So we need to somehow refactor this code in order to allow initialization of polar coordinates.
So you would typically do as you would have something like an M class called Point type where you would
have the different members so you would have Cartesian and polar and what would happen then is you would
no longer be able to have this constructor at all so instead you would have a constructor with points
A and B.
Notice I'm using a and b and not X and Y because now we don't really know what they relate to which
coordinate system and then we have a point type.
And by default we can say that is going to be a Cartesian point.
So here you would have an if statement so you would say if type is equal to point type Cartesian then
everything is fairly straightforward So you say X to A and you set Y to be otherwise.
This is a bit more tricky and in actual fact I think we need C math for this one because we're going
to do some transformations include C math in here.
So if this is the case what you would do is you would transform the coordinates so you would say x is
equal a times cause of B.
For example and you would have y equal to a Times sign of B.
So something like that.
But there is a problem with this constructor invocation as well because the names A and B are just silly.
They are not in any way helpful in figuring out what exactly you need to feed.
I mean if you're feeding in polar coordinates does a correspond to row or to theta.
So that's the question now.
I know you can add lots of documentation so you can go ahead and you can add whatever documentation
you will for the parameter.
So for the parameter a you can say this is either X or row and the other one that you can say this is
either y or theta.
So this can help a little bit but in general what's happening here is the constructor is quite simply
insufficient for describing the situation.
Now what we really want is we either want to be able to have two constructors with identical types of
arguments two floating point arguments called X Y and row 8.
Now this is impossible in C++ you can have it in objective c or in swift but in C++ that's simply not
allowed.
Alternatively one some sort of mechanism where instead of using the constructor you use something else
which can have a proper name.
So you would have a name like point from cartesian point from poler And that's exactly what the factory
pattern is ultimately all about.
It's about making sure that the interface to a particular type provides is sensible.
It's understandable it's immediately consumable by the client so they see the interface.
They know exactly how the construction of an object actually happens.