Do design pattern help us while coding? Definitely the answer is a YES. Software design patterns provide us a real world problem solution, during application development. They help us by providing features of reusable designs and interaction of objects. Before using it just for our knowledge let me share where the idea of design patterns arise from. Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) is generally considered as foundation for all design patterns.
We have sub divided the patterns into three groups – creational, structural and behavioral. So here I would like to give a small idea about Factory Design Method, which is among one of the most widely used design patterns, and it falls under the category of creational design method. So what is Factory Design Method? If we search for the exact definition for it, Gang of Four defines it as –
“Factory Method defines an interface for creating an object, but let subclasses design which class to instantiate factory method.”
Basically what it does? Where to use it? These are the questions that usually come to mind when we come across this model.
So what it does? Here we create an object without exposing its logic and we have an interface to create this object. To keep it simple it means that we create an object when it is required. You may ask we only create object for a class when it is required, unnecessary creating an object is of no use. That is quite true; here by saying “create an object when it is required” we mean if you want to create an object at runtime you can create it with factory method. So the idea is half way clear regarding what it does, isn’t it?
The second question is where to use it? Here the factory method does the magic. We can use it when object creation is required to centralize with application. Here creator class need not know how many subclasses are following it. The method returns one of several subclasses that share a common super class.
To bring to you an example in the real world from point of view of a client, if you intend to create an object and call some function (for example it do “abc” operation), if you are using factory method you don’t have to bother about where the function is written or how it will do the task “abc”. This design will be useful for you when you want to design your own framework or you are creating your own “dll”, in such cases you don’t want to expose your logic to outside world or one who use your dll. So it provides you with code security as well as encapsulation at the same time.
I hope that you are clear with an idea of factory method. To know it in depth there are various research papers available online. Go through some practical implementation, lots of programming materials are available online for factory method.
Author: Jaya Jyothy J