A gyártómintának számos előnye van.
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.
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 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; } } }
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.