Understanding the Factory Design Pattern(Virtual Constructor)
Factory Design pattern refers to a special design pattern where the developers create objects without exposing the creational logic to the client. A common interface is used to refer to the newly created objects.
This design pattern is also called the Factory Method design pattern and falls under the segment of creational design patterns. In simple terms. this pattern is all about defining an interface / abstract class for creating an object and let the subclasses decide which classes to instantiate. Factory Pattern is usually used by higher levels of a framework. Lower levels just invoke it. In this pattern, we must define a separate operation(a factory method) for creating an object. Then this method is used to create the actual objects.
Singleton vs Factory Design Pattern
- The Singleton Design pattern always returns the same instance. Factory design pattern usually accepts a parameter (when creating an object)and then the instance is decided(Could be without a parameter as well).
- In the Singleton Design pattern, we always know the instance we want. In the Factory design pattern, we don't know the instance and it is decided by the passed parameter and invoking subclasses.
Factory pattern makes the application loosely coupled and eliminates the need to bind application-specific classes into the code.
Usage
Let’s try to understand the factory pattern and how it behaves with the help of an actual real-world example.
Case study
“Shakya” is an Educational Institute located in Colombo. Shakya offers 3 main types of short-term Diploma programs for its students. Students can follow a single Diploma course at a given time.
Our application needs to dynamically create these diploma objects with required subjects according to the relevant category.
Diploma Programs and Subjects
- Software Engineering
- Software Development
- Algorithms
- Operating Systems
2. Network Engineering
- Operating Systems
- Networking
3. IT
- English
- Algorithms
Now let’s try to model a solution. we need to identify the key elements and classes of the problem.
Step 01
Each of the diploma classes contains 2–3 subjects. We need to create Diploma objects according to these relevant subjects. Therefore as the first step let’s create an abstract class “Learning” and implement subject classes which describe its course structure.
Learning class —
Networking class —
Note: Implement the classes for other subjects as well.
Step 02
Now we need to create an abstract diploma class. This class will be extended by the relevant Stream classes(Software Engineering, Networking, and It). This class also contains a method to create the relevant courses called “createCourse” that will be implemented by the subclasses.
Diploma Class —
Note: ArrayList is used to store the relevant objects (of the subjects) created by a particular sub-class.
Step 03
As the next step, we need to create the courses with their respective subjects. This is done by creating a class for each Diploma type and implementing the “createCourse” method. The objects are passed to the “subjects” list which is unique for each Diploma instance.
Network Engineering Subclass —
Note: Implement this method for other diploma subclasses as well.
Step 04
Now we need to create a Factory. A Factory that can return a new Diploma with the relevant subjects on it. Let’s implement the CourseFactory class.
Step 05
As the next step, we can implement our final class to create the actual diploma programs with the help of CourseFactory.
Output —
Now we can see that the 2 diplomas are created with relevant subjects. The subclasses decided how the parent class is created and the subjects in it. As mentioned earlier, this pattern separates the diploma construction part of the code from the actual part which uses the diploma.
Examples of Factory Design pattern in JDK
- getInstance() method of Java.util.Calendar, Numberformat, ResouceBundle.
- valueof() method in wrapper classes.
Places where we can use the Factory Method Design pattern
- Factory design patterns should be used when the developer doesn't know the exact types of dependencies that your class will work with.
- Factory design patterns can also be used when the developer needs to save system resources by reusing the already existing objects.
- This pattern can also be used to provide the users of a library or framework to extend its components.
Advantages and Disadvantages of Factory Method Design Pattern
Advantages
- Loose coupling
- Hide the implementation from the client.
Disadvantages
- The application becomes complex.
- Code is more difficult to read.
I have used the following playlist by Mr.Krishntha Dinesh to gather the required information.