English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Il modello a fabbrica è un modello di design (modello creativo) utilizzato per creare più oggetti in base ai dati forniti. In esso, creiamo un oggetto di processo di creazione astratto.
Di seguito è riportato un esempio di implementazione del modello a fabbrica. In questo esempio, abbiamo una classe interfaccia chiamata Employee e 3 classi:Studentinsegnanti,NonTeachingStaffHa implementato questo. Abbiamo creato una classe di fabbrica chiamata EmployeeFactorygetEmployee()
Questo metodo accetta un valore String e restituisce un oggetto di una delle classi specificate in base al valore String fornito.
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("Inserisci il tipo dell'oggetto che desideri: (student, lecturer, non teaching staff)"); String type = sc.next(); PersonFactory obj = new PersonFactory(); Person emp = obj.getPerson(type); emp.dsplay(); } }
Risultato di output
Inserisci il tipo dell'oggetto che desideri: (student, lecturer, non teaching staff) lecturer Questo è il metodo di visualizzazione della classe Lecturer
Sebbene si dice che ci siano cinque metodi per creare oggetti in Java-
Utilizzare la chiave di nuovo.
Utilizzare il metodo della fabbrica.
Utilizzare la clonazione.
Utilizzare Class.forName().
Utilizzare la deserializzazione degli oggetti.
L'unico modo per creare un oggetto in Java è utilizzare la chiave di nuovo, tutte le altre sono astrazioni dell'oggetto. Tutte queste metodologie utilizzano internamente la chiave di nuovo.
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("Inserisci il tipo dell'oggetto che desideri: (student, lecturer, non teaching staff)"); String type = sc.next(); EmployeeFactory obj = new EmployeeFactory(); Employee emp = EmployeeFactory.getEmployee(type); emp.dsplay(); } }
Risultato di output
Inserisci il tipo dell'oggetto che desideri: (student, lecturer, non teaching staff) lecturer Questo è il metodo di visualizzazione della classe Lecturer