Design Pattern (Part 02)— Factory Method Pattern

Photo by ekicousch on Unsplash

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.

  • 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

  1. java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods uses Factory pattern.
  2. valueOf() method in wrapper classes like Boolean, Integer etc.
  • 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.

Usage

Let’s do a simple example by using a Factory method pattern to understand how it behaves with help of a real-world example.

Case study

“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.

  1. Basic Level — Microsoft Office, Paint, Internet
  2. Knowledge Level — Internet, Web Design
  3. 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.

Leaning Class Code
MicrosoftOffice(Subject) Class code
Web Design(Subject) Class code
Graphic Design(Subject) Class code
Internet(Subject) Class code
Paint(Subject) Class code

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.

Diploma Class Code

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.

Diploma Basic Level code
Diploma Knowledge Level code
Diploma Professional Level code

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.

Course Package code
Package Code Using enum

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.

Main class code

Output:

Finally, the outcome is showing as based on the Diploma level which we were entered.

Software Engineer at Virtusa (Pvt) Ltd.