Factory concepts.

Background -

Read-only background section of Factory method design pattern (only first half) for more clarity, before you continue with this article.


First of all, what do you understand from word factory?

Simple, where we create something. Like we build cars, bikes, PC, mobiles, etc., In factories. Same way, we create software objects in the factory.

Factory concept is there to achieve creational flexibility. Factory is not only to create objects but to maintain the population of objects as well. 


Why do I need a factory to create my object?

There are many benefits of creating objects from the factory instead of from class itself. 
  • Quality assurance -
    • There will be many complex objects you will encounter in your project, which had many dependencies and cannot be created without having some dependent object in hand. If you give freedom to the developers. To create objects from wherever they want in the project, then who will guarantee the quality of a complex object (whether they get created entirely or partially?). Remembering 10 million lines of code is not possible for any developer. He/she cannot remember all dependencies of all objects in the project. Which is why we need a factory who will do the job of creation. So in case of any issue or enhancement in the creation of an object. No need to look at all code, just go to the factory and fix or enhance your code.
  • Controlling the population of objects -
    • Factory is not there only for creating the object, we can also control the population of objects if there is need (like only one object in singleton).
    • For example, we can have only 7000 logins at a time in the railway reservation portal for tatkal reservation. If 7001'th person wants to log in, then he will get an error and not able to log in until and unless another person, who has already logged in, releases the connection. 
    • What we are doing here is controlling the population of objects. If we don't do that, then as we don't have infinite resources (servers and memory) to hold that many objects, the system will crash, and no one will be able to log in. 
  • Managing the code - 
    • Project will go through from many phases (of SDLC), and in the end, it will be in the maintenance phase for soo long time. It means there will be many different engineer update/modify the code to fix many issues reported by the client. If we don't have an organized way of managing the code, it will be challenging for all stakeholders to support the product for a long time. 
    • Think all developers create objects wherever they feel in the code, then after some time it will become unmanageable code, that’s why we need a factory. It will give the object, do the cleanup. So in this way of creating object will make code more manageable and easy to maintain and understand.
  • Creating objects is not a core behavior of a class.
    • A class represents a business logic or any algorithm to solve problems. There has to be a separate responsible entity/class who should take responsibility for creating the objects as its core behavior. Instead of every other class take responsibility for creating an object.

Thanks for reading it. To learn more about design patterns and basic design principles, please see my web page.

Comments

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.

Architectural style -> Adoptable system -> Reflection.