In this article we will be talking about the factor design pattern which is also known as factory method design pattern. This design pattern comes under the Creational Design Pattern. If you guys don’t know anything about Design patterns, then click here for my previous article which will give information about Design Patterns and what Singleton Pattern is.
What is Factory Design Pattern?
Just like the singleton pattern in Factory Pattern we will not be directly creating an instance of the class using the new keyword. In singleton we would be creating a single instance of a specific class, but in factory we will be creating an instance of one or more of the subclasses created from an abstract or interface class. And these several sub classes can be instantiated in a single Factory class where the instance will be created and returned for use.
Before I talk about advantages and the differences of Factory method design compared with the singleton design, I will show a simple implementation of the factory design pattern.
For the implementation I will be implementing a system for a beauty parlor which offers different deals, in which there will be different services given. And we will be implementing the factory class for the creation of the deals. Look at the below diagram to understand the hierarchy of the system.
Each deal will be having different services, There are 4 services provided by the parlor which are haircutting, hair coloring, facial and trimming. For these I have created an abstract class called Service as seen below.
Simple abstract class Service is created. The 4 services that are provided by the beauty parlor is being extended from this class. The creation of the sub/child classes can be seen below.
Now we have the basic services that is being provided by the beauty parlor. Next we need to implement the different Deals that they offer to their customers. For that I have created an abstract class as seen below.
As you can see, the abstract class will feature an
ArrayList to hold Service objects. The
CreateDeals() method will be overridden by the child classes. The print deal method will print out the deal that the user has selected. The following are the sub classes that are created by using this abstract method.
Each Deals will be adding the services that they provide to the ArrayList object that is in the parent class. And
printDeal() method is overridden at the child class.
Note: You may be wondering how the super.printDeal() is not the first line of the method. super method needs to be first line only when calling the parent class constructor. For instance methods they can be called in any line the programmer wants.
Note :You guys may also think why createDeals() is inside the constructor. The point is that inside the parent class’s constructor the createDeal() method is called. The reason for this is that whenever we create an instance of the child class the parent class constructor will be called and the method for adding the services will be invoked automatically.
If you wish you can create any number of services and deals that you may wish, but for now lets stick with these 4 services and 3 deals. We have created the deals, now the final part is to create the Factory class which will handle creating the necessary instances.
The above is the implementation of the factory class. As you can see the method createDeal() is a static method, so it can be invoked without creating an instance of the ParlorFactory class. The next point to notice is that the method will be having a parameter, this parameter will be the one to decide which sub class of the Deal has to be returned as an object.
Note : Some might think why there isn’t a break statement in these switch cases. The reason is the program will automatically exit the method after the return statement. This is a simple mistake that I did where I got compile error 😅.
Now we have everything we need besides the main method. The following is the main method.
As you can see the main method does not hard code the objects (Not using the new keyword). Instead we will be parsing the parameter for the method in the ParlorFactory and the ParlorFactory will be deciding which child class object to return depending on the parameter that is passed. The following is the output from running the above code.
Now that we know about the Factory method design pattern lets discuss some key points about it.
- Unlike the singleton pattern in factory pattern, as seen from the above implementation we will passing in a parameter and the Factory class will decide which sub class to create and return.
- Factory Design pattern is like an overloading polymorphism which is done virtually.
- In factory pattern parent classes are not directly interacting with the users. Only the child classes are.
- As seen from the above implementation we are not seeing the initiation logic explicitly in the factory method class. The main class only sees the factory class and nothing else.
- Factory method designs are used in the higher levels of JDK. For example the NumberFormat class and the DateTime classes have
getInstance()methods where the parameter we pass will determine the object we get.
Factory pattern can be implemented in situations where different types of sub classes need to be initialized on the given input. At these types normal constructors will only make the code lengthy and difficult to read. Factory method design pattern can help in these types of situation.
Even though factory design pattern has advantages, they also come with the major disadvantage of us dealing with complex inheritance logic. It might be hard to understand when implemented in large complex situations.
Note : This is not relevant to the main topic. If you guys are running the java codes without an IDE and in your cmd. When facing the above situation where multiple files are to be compiled at once. Simple use
Click here for the GitHub link of the above implemented code segments.
The video done by Krishantha Dinesh was a huge help in creating this article