What are factories and why are factories needed in this world or why should every class except for a singleton class have atleast one public constructor?

What are factories and why are factories needed in this world or why should every class except for a singleton class have atleast one public constructor?

Technically factories are systems who are responsible for creating a particular type of object. In this world there are a lot of factories which are responsible for creating a particular type of objects

For example

  • Car Factory is responsible for creating different types of Cars.
  • Bike Factory is responsible for creating different types of Bikes.
  • Soap Factory is responsible for creating different types of Soaps.
  • Shampoo Factory is responsible for creating different types of Shampoos.
  • etc

Now comes the next important question … "Why do we need factories in this world?"

Just think about it ….

  • I am capable of creating my own car … then why do I still purchase it from a factory.
  • I am capable of creating my own bike … then why do I still purchase it from a factory
  • I am capable of creating my own fan … then why do I still purchase it from a factory.
  • I am capable of creating my own soap … then why do I still purchase it from a factory.
  • I am capable of creating my own aero plane … then why do I still purchase it from a factory.

Friends … it might sound unbelievable but each one of us is capable of creating each of the products/systems that we today purchase from a factory. It might be a different fact that people might not be aware of their capabilities … but the technical fact stands that each one of us is capable of creating any product/ system and the knowledge of creation is mostly publicly available as well. For people who have studied science and engineering they are definitely capable of doing the same. One of the reasons people doubt their capability of creating any thing is … because of the availability of factories ..there was no need for a person to think about it.

Atleast software engineers can understand it in the right way ….

Ask yourself why does every Class except for a Singleton class has atleast one public constructor?

The reason is … if your software systems are automating the real world in the true sense then like classification , it should follow all the rules of the real world as well. As I have just showed … any one in this world should be allowed to create an object of any class, in the real world this is ensured by making the knowledge of creation public … in software systems this is enabled by making the constructor public so that anyone can just call the constructor of the class and create an object of the same.

At least software engineers should now be able to appreciate "Why every class except for the Singleton class should have atleast one public Constructor?"

Now if a every class except for a singleton class has atleast one public constructor.. using which an instance of a class can be created … Then why do we need factories to create objects of a particular type?

First let me explain the same for real world systems and then I will explain for software systems.

There are many reasons for a person to purchase a product/ system from a factory although he himself is capable of creating it.

Although I am capable of creating my own car … but still I purchase it from a factory for the following reasons

  • because the factory knows the best way of creating the particular type of object / product/ system which I might not be aware of.
  • because the factory knows the safest way of creating the particular type of object / product/ system which I might not be aware of.
  • because the factory knows the most cost effective way of creating the particular type of object / product/ system which I might not be aware of.

This is one set of reasoning that can justify myself purchasing products/ systems from a factory although I was myself capable of creating the same.

The second convincing reason behind this is explained by the concept of "Core and Non Core behaviors". I have discussed in various articles about the fundamental design principle of any well designed system which says that "A well designed system should only have its core behaviors … any non core behavior that a system is forced to have will become a liability on the system.". We know every behavior within a system has an enabling structure .We also know "Core Behavior" is a behavior which is needed at all points of time and hence the enabling structure will never become a liability on the system. While a "Non Core behavior" is a behavior which is not needed at all points of time and hence any enabling structure for the non core behavior will become a liability on the system when the behavior is not needed.

Now think about … Creating an object is also a behavior of a system which needs enabling structure. For example for a car to be created certain amount of raw materials, machines and tools would be needed . Now if I have the need to have a new car every other day for which a new car has to be created every day … purchasing all the machinery and tools make sense as these enabling structures will always be used as "Creation of a car" is a core behavior and this enabling structure will never be a liability on my system. But what if I would only need to have and use only one or two or maximum of three cars in my life time .. In this case would purchasing all the machinery and tools make sense? don't you think that these machinery and tools are useless when this behavior is not needed.

Hence it is not advisable for systems to have the behavior of creating other objects because it might not be a core behavior of the system and hence any enabling structure needed for creation of the object will become a liability on the system. While on the other hand because the job of the factory is just to create a particular type of object / system/ product … any enabling structure for the creation of these objects will always be used and will never become a liability on the factory as a system.

So this is also a very important reasons for factories to exist in this world and will justify people purchasing objects from a factory although they themselves were capable of creating the same.

let us understand … all of the above mentioned reasoning's make sense and can justify the need of a factory to create objects …but we are still missing the most important point describes the need behind factories.

Let us understand this point through the following example.

Although I am capable of creating of my own cars and an aero plane … but if I create the same without getting a license from the government .. I will be put behind the bars.

Surprised … Yes … In India and it should also be true in other countries in this world as well … creating any kind of objects without a license for the same is a legal offence.

Now think about it … why do we need to take a license form the government to create objects?

One of the first thing that immediately comes to mind is "safety" … let say I am creating my own car / aero plane without using appropriate engineering best practices and tools resulting in a system of poor quality and hence the system crashes into a building killing innocent people . Should this be allowed?

No … This is one of the reason why licenses are needed from the government.

Very recently in rural India … I heard of a story mentioning a mechanic created a helicopter using the engine of the scooter… The government didn't allow him to fly the same and a lot of people were amused at the governments decision. But the fact is had that helicopter crashed while testing leading to casualties on ground … who should have been responsible for it?

So definitely safety is one of the reasons… but we are still missing the most important reason.

Let us presume a hypothetical case that government of India allows each of citizens to create their own cars and because of this each and every citizen has created one or more car from themselves. I sure we all know that every object within a system will always hog some system resources like space. Even a system like car needs occupies space like parking space and driving space on roads. Now since each of the citizens are capable of creating their own cars … people start creating cars at will and one fine day the government of India realizes that there are too many cars on the Indian roads leading to severe traffic congestions and the roads don't any more resources to handle more cars. So can the government do anything to stop the inflow of the cars on the roads. No … because the government has allowed each one of the citizens to create their own cars … they just cannot control the number of cars that are being created leading to the failure of system because of uncontrolled creation of objects and depleting resources.

So now can we guess why are factories needed in this world and why doesn't government allow each and every one to create their own objects?

The answer is factories are needed in this world so as to control the number of objects in a particular system.

So in this case … so we have dedicated factories in this country who are responsible for creating cars and the government has given them the license to do it ..

So if Government of India realizes that there are too many cars in India and Indian roads cannot handle any further cars … they can simply instruct the factories to slow down the production or stop the production for sometime so as to control the number of objects within systems there by preventing the overall system to collapse due to lack of resources.

We can conclude factories are needed in this world so as to control the number of instances/ objects within a particular system there by maintaining an equilibrium between the number of various types of objects belonging to different classes. Remember uncontrolled creation of a system can also make a system fail due to lack of resources while scarcity of certain type of objects can also lead to dearth of critical objects making the system fail.

Hence we will see "For all the non living entities in this world …. we have factories for the same … and for the entities where don't have a factory [living entities] … we have a big problem brewing in this world"

Excellent example for the same are

Too many human beings as a result of uncontrolled creation of human beings ….. results in population explosion … Let us understand every human being as an object occupies/uses a certain set of limited resources available with the country. If the number of objects increase at an exponential rate in a uncontrolled manner it will definitely result in the scarcity of critical resources leading the system to a failure. There are so many who are trying to control population explosion.

Its not just with human beings every day one sees a lot of reports about uncontrolled creation of dogs, monkeys, rats creating a huge menace in various localities there by resulting in system failures.

Had there been a HumanFactory or a DogFactory or MonkeyFactory .. the respective government would have just asked these factories to slow down the production or stop the production temporarily to manage the resources of a system.

I have just shown you examples wherein uncontrolled creation is creating scarcity of resources .. they by leading to systems to fail while on the other hand there are certain living objects whose number is falling at a exponential rate thereby making the system fail.

Trees, Tigers, Certain Species of wildlife reducing at a drastic rate is also making the eco system fail.

Had there been a TreeFactory or a TigerFactory or WildLifeFactory .. the respective countries would have just asked these factories to increase the production rate to compensate for the reduction of these scarce objects thereby maintaining the equilibrium of the system.

In a nutshell factories as systems are needed in this world so as to control the number of objects of a particular type so as to maintain the appropriate number of each type of objects within a system.

The same are the reasons for the usage of factories within software systems as well.

As we know every software object will also use certain resources like "memory" of the address space which is limited. Think about it, if every one is allowed to create objects on their own using the public constructor of different classes, it will lead to uncontrolled creation of objects within the address space thereby leading the systems to fail. Hence factories are needed in software systems so as to control and maintain the equilibrium of the different types of software objects to deliver the needed functionalities while conserving system resources.

 
Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer

www.VPlanSolutions.co.in