静态工厂方法在内部是否使用 new 关键字在 Java 中创建对象?
工厂模式是一种设计模式(创建模式),用于根据我们提供的数据创建多个对象。在其中,我们创建一个抽象化的创建过程对象。
示例
下面是工厂模式的示例实现。此处,我们有一个名为 Employee 的接口和 3 个类:实现它们的Student、Lecturer 和NonTeachingStaff。我们创建了一个工厂类 (EmployeeFactory),其中包含一个名为 getEmployee() 的方法。此方法接受一个 String 值并根据给定的 String 值返回其一类的对象。
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()。
- 使用 object 反序列化。
在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP