We can provide a meaningful name for our constructors.
We can provide several constructors with the same number and type of parameters, something that as we saw earlier we can’t do with class constructors.
public class RandomIntGenerator {
private final int min;
private final int max;
private RandomIntGenerator(int min, int max) {
this.min = min;
this.max = max;
}
public static RandomIntGenerator between(int max, int min) {
return new RandomIntGenerator(min, max);
}
public static RandomIntGenerator biggerThan(int min) {
return new RandomIntGenerator(min, Integer.MAX_VALUE);
}
public static RandomIntGenerator smallerThan(int max) {
return new RandomIntGenerator(Integer.MIN_VALUE, max);
}
public int next() {...}
}
We can avoid providing direct access to resource intensive constructors, like for databases. public class DbConnection { private static final int MAX_CONNS = 100; private static int totalConnections = 0;
private static Set<DbConnection> availableConnections = new HashSet<DbConnection>();
private DbConnection()
{
// ...
totalConnections++;
}
public static DbConnection getDbConnection()
{
if(totalConnections < MAX_CONNS)
{
return new DbConnection();
}
else if(availableConnections.size() > 0)
{
DbConnection dbc = availableConnections.iterator().next();
availableConnections.remove(dbc);
return dbc;
}
else {
throw new NoDbConnections();
}
}
public static void returnDbConnection(DbConnection dbc)
{
availableConnections.add(dbc);
//...
}
}
The static factory method is a variation of the factory method pattern. It is used to create objects without having to call the constructor yourself.
When to use the Static Factory Method
There are some disadvantages like
Example
Pizza.cs
public class Pizza
{
public int SizeDiameterCM
{
get;
private set;
}
private Pizza()
{
SizeDiameterCM = 25;
}
public static Pizza GetPizza()
{
return new Pizza();
}
public static Pizza GetLargePizza()
{
return new Pizza()
{
SizeDiameterCM = 35
};
}
public static Pizza GetSmallPizza()
{
return new Pizza()
{
SizeDiameterCM = 28
};
}
public override string ToString()
{
return String.Format("A Pizza with a diameter of {0} cm",SizeDiameterCM);
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
var pizzaNormal = Pizza.GetPizza();
var pizzaLarge = Pizza.GetLargePizza();
var pizzaSmall = Pizza.GetSmallPizza();
String pizzaString = String.Format("{0} and {1} and {2}",pizzaSmall.ToString(), pizzaNormal.ToString(), pizzaLarge.ToString());
Console.WriteLine(pizzaString);
}
}
Output
A Pizza with a diameter of 28 cm and A Pizza with a diameter of 25 cm and A Pizza with a diameter of 35 cm