bean factories - technoangel/java GitHub Wiki
Bean Factories
Static Factory Methods
Static factory methods are used to create a bean using business logic as well as arguments. This is a well-documented design pattern around private constructors.
public class Organization {
private String companyName;
private int yearOfIncorporation;
private Organization(String companyName, int yearOfIncorporation) {
this.companyName = companyName;
this.yearOfIncorporation = yearOfIncorporation;
}
public static Organization createInstance(String companyName, int yearOfIncorporation) {
System.out.println("Invoking static factory method");
return new Organization(companyName, yearOfIncorporation);
}
...
}
Instance Factory Method
This manner of instantiation involves a dedicated class that will return the instance of the desired object. This is just like Static Factory Methods (above) but it requires two classes.
public class OrganizationFactory {
public void getInstance(String companyName, int yearOfIncorporation) {
System.out.println("Invoking instance factory method");
return new Organization(companyName, yearOfIncorporation);
}
}