English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

静的方法がJavaでnewキーワードを使用して内部でオブジェクトを作成するのは静的方法ですか?

工場パターンは、私たちが提供するデータに基づいて複数のオブジェクトを作成するためのデザインパターン(クリエイティブパターン)です。その中で、抽象的な作成プロセスのオブジェクトを作成しています。

以下に工場パターンの例を実装しています。ここでは、名前のEmployeeと3クラスのインターフェース:Student、講師、NonTeachingStaff、それを実現しました。名前のクラスを使用して工場クラス(EmployeeFactory)を作成しました。getEmployee()。このメソッドはString値を受け取り、指定されたString値に基づいてそのクラスのオブジェクトの1つを返します。

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
基本チュートリアル
おすすめ