Digital Magpie

Ooh, ooh, look - shiny things!

Factory Methods vs. Factory Classes in Java

There are a number of cases in Java where I find myself wanting to use a variant of the factory method pattern. For example:

  • To control the number of objects created, or to create a limited set of objects (one per value of an enumeration for example, this could use an backed map in J2SE 5);

  • To perform some checks before creating object. Like init() called before instantiating instead of after.

Often my instinct here is to add some static methods to the class and make the constructors private. I think that in part this comes from the pattern name: factory method, I think a better name would have been factory class, or maybe just factory. Anyway, doing this has some advantages in that it reduces the number of classes that need writing and keeps all of the code in one place. Like the image to the right.

But there are drawbacks as well. The main one being that I cannot then treat the factory like a bean, this makes it more difficult to display in UI (by preventing the use of common binding frameworks for example). So a better approach is more like the image to the left.

Note that this is still one class short of the pattern described in the Wikipedia article. The Creator interface is just unnecessary as far as I’m concerned (although there is a case for it’s existence in scenarios like the various Java SPI-style interfaces).