What does the factory pattern do for you in Java, and when is it better than calling a constructor directly?
A factory is a method that decides which class to make. The caller says what it wants in a way that does not name the class, and the factory works out which class to build.
public class Main {
public static void main(String[] args) {
Notification n = NotificationFactory.create("email");
System.out.println(n.send("hello"));
}
}
And here is the factory.
public final class NotificationFactory {
private NotificationFactory() {
}
public static Notification create(String kind) {
if (kind.equals("email")) {
return new EmailNotification();
}
if (kind.equals("sms")) {
return new SmsNotification();
}
throw new IllegalArgumentException("unknown notification: " + kind);
}
}
And when is it better than calling the constructor directly? There are four reasons, and the first one is the biggest.
the reason what it does
---------------------------------- --------------------------------------------
the class name is a decision the factory decides, and the
caller does not know
the constructor is private the class controls how its
objects are made
the setup is in one place every object is built the same
way
there are extra steps the object is created and then
configured
And the first one is worth showing, because it is the case that comes up most. So a notification has to be sent by email, by SMS or by push, and the caller says which one. With a direct constructor the caller has to know the class name and it has to know all of them.
// The caller has to know the classes.
if (kind.equals("email")) {
n = new EmailNotification();
} else if (kind.equals("sms")) {
n = new SmsNotification();
}
And with the factory the decision is in one place, so a new kind of notification is one new branch in the factory and the callers do not change.
And the third reason is worth explaining, because it is the one that the static factory method gives you for free. So the constructor can be private and then the class is the only thing that can make its own objects. That means it can guarantee that every object is set up properly.
public class Money {
private final long amountInCents;
// The constructor is private, so the only way to make one
// is through the factory method.
private Money(long amountInCents) {
this.amountInCents = amountInCents;
}
public static Money of(long amountInCents) {
if (amountInCents < 0) {
throw new IllegalArgumentException("amount cannot be negative");
}
return new Money(amountInCents);
}
}
And the fourth reason is about objects that need extra setup. So an object that needs a connection, or a file, or a registered handler can be made and then configured inside the factory, and the caller never sees a half-built object.
public static Connection open(String url) {
Connection connection = new Connection(url);
connection.setTimeout(5000);
connection.setRetries(3);
return connection;
}
So the rule to remember is this. A factory moves the decision about which class to make out of the caller and into one place.
The last question in this topic is about when to use one and what the alternatives are.