静态工厂方法是否在内部使用new关键字在Java中创建对象?
工厂模式是一种设计模式(创意模式),用于根据我们提供的数据创建多个对象。在其中,我们创建了一个抽象创建过程的对象。
示例
下面给出了工厂模式的示例实现。在这里,我们有一个名为Employee和3类的接口:Student,讲师,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()。
使用对象反序列化。
用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
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语