Factory Design Pattern (C#)

A factory is a component responsible for the wholesale (not piecewise) creation of objects.

Let’s talk about motivation to use “Factory Design Pattern

  1. Object Creation logic becomes too convoluted.

  2. The constructor is not descriptive


There are lots of limitation like:

  1. The name of the constructor has to match the name of the containing type, so you cannot communicate additional information that how the object is constructed.

  2. Cannot overload constructor with the same sets of arguments with different names

  3. Object Creation (Non — piecewise, unlike Builder) can be outsourced to (A separate function( Factory Method)).

  4. That may exist in a separate class (Factory)

  5. Can create a hierarchy of factories with Abstract Factory


One of the reasons why factories exist is because constructors are not that good, Let's take a look at the problem by taking a real example.

We are going to make a class named as the point which will represent the point x and y coordinates as field and make the constructor where we initialize the coordinates.

Now what happens after bit of a time we realize that we need to add polar coordinates also,so here comes the problem, now we can't be able to overload the same types in polar coordinates you can see in the screenshot we are getting the issue with red line, because as you can see that signature is exactly the same as the point above.

Issue While Overload Pic 1.1


so now the question is how to resolve this issue and the issue is how to initialize a point from both to cartesian coordinates as well as polar coordinates, so here we can start using enums named as Coordinates where we can define two types cartesian and Polar types, so now we end up doing additional arguments in constructor which specify the coordinate system and we as well give the default value as cartesian points.

Now we already have a couple of problems here, because we can no longer have the arguments as x & y neither we can name them as “rho” and “theta” so we have to create some mutual named as a & b please refer to the screenshot we changed the parameter name from x and y to a &b we no longer need that as we don’t know in the runtime what coordinates point will going to be used

Changed the signature with a and b, and by default, a coordinate is cartesian Pic 1.2


as in the case of the Cartesian system, it’s easy for the polar coordinates we added some formula. so here there is no indication of Cartesian coordinates or there is no clear specification which point is using from which coordinates.

So we have to add XML comments (refer to the screenshot pic 1.3) to let people understand how you are using a&b parameters what basically are they. so that is a bit inconvenient, Still, we are bound to the name Point because constructor name should be matched to the class name, so we have the precise problem which can be resolved by the factory pattern,

here are the XML comments to let the user know about parameters Pic 1.3