Why we need to use “Factory” design method?
The factory method is a creational design pattern, meaning it is concerned with the production of objects.
In the Factory pattern, we create an object without revealing the development logic to the client, and the client creates a new form of an object using the same generic interface. The idea is to use a static member function (static factory method) that creates and returns instances while keeping class module details hidden from the user.
A factory pattern is one of the key design concepts for creating an object, allowing clients to build library objects that aren't tightly coupled with the library's class hierarchy.
In addition, the Factory Method pattern is generally used in the following situations:
A class cannot anticipate the type of objects it needs to create beforehand.
A class requires its subclasses to specify the objects it creates.
You want to localize the logic to instantiate a complex object.
Important: When developing some software project if the developer knew what are the instance need to be inside the class already, and if it did not need to create multiple types of objects, at that stage it did not need to create that project using this factory method.
Factory design pattern real world using example
Let’s take an example scenario;
Think we have a large amount of a dataset of some social media platform, and we need to categorize that dataset like (according to time),
Hot data - every time accessed data (the last 5hr used data)
Warm data - sometimes accessed data (low-frequency usage rather than Hot data) (the last 24hr used data, without last 5hr)
Cold data - rarely accessed data (also can name as the history of data usage) (last year or before used data)
Now we need to develop some application for that, Client (or the application user) only passed to that application the timeline or a time range of that data, according to that input the application needs to have the ability to retrieve that data (matches to the user entered time) from the relevant data source.
To design that type of application structure developer can use this Factory method. To do that let’s think we store that data like,
Hot data - Caching memory
Warm data - Mongo database
Cold data - Some big data store own to the client company
According to the user passing time period, it needs to create a specific object that has the ability to retrieve the data from the relevant data source, or else this can explain as, the client doesn’t care where these data coming from, the only clue they passing to the factory is the time period using that time factory decide the object (create an instance or a driver) that has the ability to give relevant data to the client.
4.1 UML for Factory Method Pattern above example scenario
Java implementation for Factory Method Pattern above example scenario
Note: This implementation does not include real coding lines and all exact methods this is just a scratch of how this factory method works. (Demo purpose only)
-----------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
package factory_example;
public interface Data_Retriever {
void getData();
}
----------------------------------------------------------------------------------------------------------------------------------
package factory_example;
public class Hot_Data_Retriever implements Data_Retriever {
@Override
public void getData()
{
System.out.println("Retrieve data from Caching memory");
}
}
----------------------------------------------------------------------------------------------------------------------------------
package factory_example;
public class Warm_Data_Retriever implements Data_Retriever {
@Override
public void getData()
{
System.out.println("Retrieve data from Mongo database");
}
}
----------------------------------------------------------------------------------------------------------------------------------
package factory_example;
public class Cold_Data_Retriever implements Data_Retriever {
@Override
public void getData()
{
System.out.println("Retrieve data from Big data store");
}
}
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
package factory_example;
import java.sql.Time;
public class Get_Data {
//use getData method to get object to access relevant data sets
public Data_Retriever getData(Time selectedTimeRange)
{
if(timeCheck(selectedTimeRange)) //use timeCheck method to validate user selected time range
{
return null;
}
/*use dataSourcesSelector method to find and select the user entered time rage correctly
example: if user entered time rage belongs to last 5hr it return integer 1,
that means it direct to the Hot_Data_Retriever...
*/
if(dataSourceSelector(selectedTimeRange) == 1)
{
return new Hot_Data_Retriever();
}
else if(dataSourceSelector(selectedTimeRange) == 2)
{
return new Warm_Data_Retriever();
}
else if(dataSourceSelector(selectedTimeRange) == 3)
{
return new Cold_Data_Retriever();
}
return null;
}
}
----------------------------------------------------------------------------------------------------------------------------------
package factory_example;
public class Select_Time_Range {
public static void main(String[] args) {
Get_Data get_data = new Get_Data();
//get an data from Hot data set
Data_Retriever data_set = get_data.getData(); //pass time range inside this brackets
//retrieve that data
data_set.getData();
}
}
-----------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
0 Comments