English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The factory pattern is a design pattern (creational pattern) used to create multiple objects based on the data we provide. In it, we create an object of an abstract creation process.
Below is an example implementation of the factory pattern. Here, we have a class named Employee and3class interface:Student,Lecturer,NonTeachingStaff,实现了它。我们使用名为的方法创建了一个工厂类(EmployeeFactory)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 display(); } class Student implements Person { public void display() { System.out.println("This is display method of the Student class"); } } class Lecturer implements Person { public void display() { System.out.println("This is display method of the Lecturer class"); } } class NonTeachingStaff implements Person { public void display() { 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 Result
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is the display method of the Lecturer class
Although it is said that there are five methods to create objects in Java-
Use the new keyword.
Use the factory method.
Use cloning.
Use Class.forName().
Use object deserialization.
The only way to create an object in Java is to use the new keyword, and all other methods are abstractions of the object. All these methods internally use the new keyword completely.
import java.util.Scanner; interface Employee { void display(); } class Student implements Employee { public void display() { System.out.println("This is display method of the Student class"); } } class Lecturer implements Employee { public void display() { System.out.println("This is display method of the Lecturer class"); } } class NonTeachingStaff implements Employee { public void display() { 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 Result
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is the display method of the Lecturer class