Design Pattern (Part 02)— Factory Method Pattern
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.
The Factory Method Pattern is also known as Virtual Constructor.
Advantage of Factory Design Pattern
- Factory Method Pattern allows the sub-classes to choose the type of objects to create.
- It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class so that it will work with any classes that implement that interface or that extend that abstract class.
Factory Design Pattern Examples in JDK
- java.util.Calendar, ResourceBundle and NumberFormat
getInstance()methods uses Factory pattern.
valueOf()method in wrapper classes like Boolean, Integer etc.
Usage of Factory Design Pattern
- When a class doesn’t know what sub-classes will be required to create
- When a class wants that its sub-classes specify the objects to be created.
- When the parent classes choose the creation of objects to its sub-classes.
Let’s do a simple example by using a Factory method pattern to understand how it behaves with help of a real-world example.
“Asoft Technologies” is an Computer Educational Institute located in Colombo, SriLanka. Asoft Technologies offering 3 types of short-term Diploma level programs for after ordinary level school students. Every students can follow a single diploma course at a course duration time. Below in this application we are going to create some diploma level objects with rewuired subject based on the each diploma levels.
Diploma Level Programs and Subjects
- Basic Level — Microsoft Office, Paint, Internet
- Knowledge Level — Internet, Web Design
- Professional Level — Graphics designing, paint
Now let’s try to find a solution. we need to identify the key elements and classes of the problem.
If we see our system structure there are 3 levels and each level has some subjects according to the level bacis. So we need to create Diploma Level objects according to the relevant subjects. Therefore the first stop was we have to create an abstract class “Learning” and implement subject classes which are in our diploma level structure.
Once created the all class file for subjects next have to create an abstract diploma class and at the same time, it should be extended by the relevant classes(Basic Level, Knowledge Level, and Professional Level). This class also contains a method to create the relevant courses called “createCourse” that will be implemented by the subclasses
Now we need to create an abstract diploma class. This class will be extended by the Diploma Level classes(Basic Level, Knowledge Level, and Professional Level). This class also contains a method to create the relevant courses called “createCourse()” that will be implemented by the subclasses.
Note: ArrayList is used to store the relevant subject objects created by a particular sub-class.
If we come to the third step, let's create the subjects for each Diploma level. 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.
Now we have to create a course package class. This will return a Diploma level with the relevant subject. The below picture shows the CoursePackage code.
This package code using for the switch case.
Finally, we can implement our main class(Asoft Technologies class) to find the subject details based on the level at which we were selected with help of CoursePackage.
Finally, the outcome is showing as based on the Diploma level which we were entered.
Factory Method Design Pattern - Javatpoint
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object…