[[oktatas:programozás:java:java_tervezesi_mintak|< Java tervezési minták]] ====== Factory ====== * **Szerző:** Sallai András * Copyright (c) 2014, Sallai András * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== Bevezetés ===== A gyártómintának számos előnye van. * Könnyen olvasható kód. * Rugalmasan beállítható attribútumok. * Könnyen beállítható alapértelmezett értékek. * Könnyű bővíthetőség. * Interfészekkel kompatibilis származtatott osztályok. Teljesül az egyetlen felelősség elve, és a nyitott/zárt elv. A hátránya lehet, hogy a kód bonyolultabbá válhat. ===== Gyártó metódus ===== A gyártómetódusok neve kezdődhet például: create, make, build szavakkal. public class Employee { String name; String city; double salary; private Employee() {} public static Employee makeEmployee() { return new Employee(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } public class App { public static void main(String[] args) throws Exception { Employee emp = Employee.makeEmployee(); emp.setName("Nagy János"); emp.setCity("Szeged"); emp.setSalary(389); } } ===== Gyártó osztály ===== public class Employee { private String name; private String city; private double salary; private Employee() {} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", city='" + city + '\'' + ", salary=" + salary + '}'; } public static class EmployeeBuilder { private Employee employee; public EmployeeBuilder() { this.employee = new Employee(); } public EmployeeBuilder setName(String name) { employee.name = name; return this; } public EmployeeBuilder setCity(String city) { employee.city = city; return this; } public EmployeeBuilder setSalary(double salary) { employee.salary = salary; return this; } public Employee build() { return employee; } } } public class App { public static void main(String[] args) throws Exception { Employee emp = new Employee.EmployeeBuilder() .setName("Nagy János") .setCity("Szeged") .setSalary(398) .build(); System.out.println(emp.toString()); } } ===== Elvont gyártó ===== Dolgozók adatait szeretnénk nyilvántartani. Jelenleg kétféle szerep létezik: fejlesztők és tesztelők. Szeretnénk, ha ezt az információt le lehetnek kérdezni. public abstract class Employee { private String name; private String city; private double salary; public Employee(String name, String city, double salary) { this.name = name; this.city = city; this.salary = salary; } public abstract String getRole(); @Override public String toString() { return name + " " + city + " " + salary; } public String getName() { return name; } public String getCity() { return city; } public double getSalary() { return salary; } } public abstract class EmployeeFactory { public abstract Employee createEmployee(String name, String city, double slaray); } public class Developer extends Employee { public Developer(String name, String city, double salary) { super(name, city, salary); } @Override public String getRole() { return "Developer"; } } public class DeveloperFactory extends EmployeeFactory{ @Override public Employee createEmployee(String name, String city, double salary) { return new Developer(name, city, salary); } } public class Tester extends Employee { public Tester(String name, String city, double salary) { super(name, city, salary); } @Override public String getRole() { return "Tester"; } } public class TesterFactory extends EmployeeFactory { @Override public Employee createEmployee(String name, String city, double slaray) { return new Tester(name, city, slaray); } } public class App { public static void main(String[] args) throws Exception { EmployeeFactory developerFactory = new DeveloperFactory(); Employee developer = developerFactory.createEmployee("Erős István", "Szeged", 385); EmployeeFactory testerFactory = new TesterFactory(); Employee tester = testerFactory.createEmployee("Csendes István", "Szolnok", 385); System.out.println(developer.toString() + " " + developer.getRole()); System.out.println(tester.toString() + " " + tester.getRole()); } } Ha új szerepet kell felvenni, a többit nem kell változtatni. Ha új tulajdonságot kell tárolni, akkor az EmployeeFactory-ban megadom, és a kódszerkesztő figyelmeztet melyik fájlban kell szerkeszteni még. Az EmployeeFactory lehet interfész is. ===== Lásd még ===== * [[oktatas:programozas:tervezesi_mintak:gyarto_metodus|Gyártó metódus]] ===== Linkek ===== * https://okt.inf.szte.hu/prog1/gyakorlat/eloadas/TervezesiMintak/gyartasiMintak/ (2023) * https://refactoring.guru/design-patterns/factory-method (2023) * https://www.digitalocean.com/community/tutorials/factory-design-pattern-in-java (2023)