靜態工廠方法在內部是否使用 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

更新時間: 2020 年 7 月 2 日

141 次瀏覽

提升您的職業生涯

完成課程,獲得認證

開始
廣告