
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Static Factory Method and New Keyword in Java
Factory pattern is a design pattern (creational pattern) which is used to create multiple objects based on the data we provide. In it we create an object abstracting the process of creation.
Example
Below given is the example implementation of the factory pattern. Here, we have an interface with name Employee and 3 classes: Student, Lecturer, NonTeachingStaff, implementing it. We have created a factory class (EmployeeFactory) with a method named getEmployee(). This method accepts a String value and returns an object of one of the classes, based on the given String value.
import java.util.Scanner; interface Person{ void dsplay(); } class Student implements Person{ public void dsplay() { System.out.println("This is display method of the Student class"); } } class Lecturer implements Person{ public void dsplay() { System.out.println("This is display method of the Lecturer class"); } } class NonTeachingStaff implements Person{ public void dsplay() { System.out.println("This is display method of the NonTeachingStaff class"); } } class PersonFactory{ public Person getPerson(String empType) { if(empType == null){ return null; } if(empType.equalsIgnoreCase("student")){ return new Student(); } else if(empType.equalsIgnoreCase("lecturer")){ return new Lecturer(); } else if(empType.equalsIgnoreCase("non teaching staff")){ return new NonTeachingStaff(); } return null; } } public class FactoryPattern { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the type of the object you want: (student, lecturer, non teaching staff)"); String type = sc.next(); PersonFactory obj = new PersonFactory(); Person emp = obj.getPerson(type); emp.dsplay(); } }
Output
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class
Static factory method
Though it is said that there are five ways to create an object in Java namely −
- Using the new keyword.
- Using the Factory method.
- Using cloning.
- Using Class.forName().
- Using object deserialization.
The only way to create an object in Java is using the new keyword all other ways are abstractions to it. Intact all these methods internally use the new keyword.
Example
import java.util.Scanner; interface Employee{ void dsplay(); } class Student implements Employee{ public void dsplay() { System.out.println("This is display method of the Student class"); } } class Lecturer implements Employee{ public void dsplay() { System.out.println("This is display method of the Lecturer class"); } } class NonTeachingStaff implements Employee{ public void dsplay() { System.out.println("This is display method of the NonTeachingStaff class"); } } class EmployeeFactory{ public static Employee getEmployee(String empType) { if(empType == null){ return null; } if(empType.equalsIgnoreCase("student")){ return new Student(); } else if(empType.equalsIgnoreCase("lecturer")){ return new Lecturer(); } else if(empType.equalsIgnoreCase("non teaching staff")){ return new NonTeachingStaff(); } return null; } } public class FactoryPattern { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the type of the object you want: (student, lecturer, non teaching staff)"); String type = sc.next(); EmployeeFactory obj = new EmployeeFactory(); Employee emp = EmployeeFactory.getEmployee(type); emp.dsplay(); } }
Output
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class