English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
工場メソッドは、私たちが提供するデータに基づいて複数のオブジェクトを作成するためのデザインパターン(創造パターン)です。その中で、抽象的な作成プロセスのオブジェクトを作成します。
以下に工場メソッドの例を実装しています。ここでは、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(); } }
output result
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(); } }
output result
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class