静态工厂方法在 Java 中是否内部使用 new 关键字来创建对象?
java 8object oriented programmingprogramming
工厂模式是一种设计模式(创建模式),用于根据我们提供的数据创建多个对象。在其中,我们创建一个抽象创建过程的对象。
示例
下面给出了工厂模式的示例实现。这里,我们有一个名为 Employee 的接口和 3 个类:Student、Lecturer、NonTeachingStaff,实现它。我们创建了一个工厂类(EmployeeFactory),其中有一个名为 getEmployee() 的方法。此方法接受一个字符串值,并根据给定的字符串值返回其中一个类的对象。
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(); } }
输出
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class
静态工厂方法
虽然据说在 Java 中有五种方法可以创建对象,即 −
- 使用 new 关键字。
- 使用工厂方法。
- 使用克隆。
- 使用 Class.forName()。
- 使用对象反序列化。
在 Java 中创建对象的唯一方法是使用 new 关键字,所有其他方法都是对它的抽象。所有这些方法在内部都使用 new 关键字。
示例
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(); } }
输出
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class