20

I don't remember exactly is it a common pattern but I have a class (Factory Method pattern) which has method for creating other classes (Abstract Factory pattern) depending on enum parameter:

public class FooFactoryFactory {
   public FooFactory createFactory (FooFactoryType type) {
      switch (type) {
         case AFoo:
            return new AFooFactory ();
            break;
         case BFoo:
            return new BFooFactory ();
            break;
         default:
            throw new RuntimeException ("...");
      }  
   }
}

public interface FooFactory {
   Foo createFoo ();
   FooItem createFooItem ();
}

FooFactory has several implementations as well as Foo interface and FooItem interface (common Abstract Factory pattern).

So, how to rename FooFactoryFactory?

Maybe, FooFactoryCreator? (Think of this name during writing this question). IMHO it's nice, how do you think?

Roman
  • 64,384
  • 92
  • 238
  • 332
  • 3
    `FooFactoryFactory` may sound silly, but I think it communicates clearly what it is. Hopefully you are unlikely to ever need a `FooFactoryFactoryFactory` :-) – Eldritch Conundrum Jun 25 '14 at 11:42

3 Answers3

22
  • FooFactoryCreator
  • FooFactoryProvider

But you might want to rename your factories to, say, builders. Take a look at javax.xml.parsers.DocumentBuilderFactory, which procudes DocumentBuilder instances, which in turn produce Documents

Looking into the DocumentBuilderFactory example, another option arises:

  • have an abstract FooFactory
  • make a static newInstance() method there (with parameters)
  • let newInstance() return the appropriate implementation of FooFactory
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 4
    +1: Nice suggestions and alternatives. I was myself immediately thinking about `MotherFooFactory` :) – BalusC Feb 02 '10 at 18:21
  • 3
    the `Mother` convention is the one I'm using the next time I need this :) – Bozho Feb 02 '10 at 18:25
  • 2
    Agreeable solution, only comment is a word of caution that only in rare instances is a "factory of factories" a truly necessary design. It sounds convoluted, and I'd be surprised if there weren't a more elegant pattern to apply to whatever the problem is. Case-in-point, DocumentBuilderFactory is so often misused (especially from a thread safety perspective), that I would immediately question any code of mine that could be compared to it. – Justin Searls Feb 03 '10 at 18:15
  • To me, Factories create look-a-like objects while Builders allow much more control over customization of the object being built before building it. For example, DocumentBuilder allows you to set number of properties about the document before building it while DocumentBuilderFactory creates a Factory implementation that can be used to create DocumentBuilder (with little to no customization). – NawaMan Jun 12 '12 at 19:49
4

In Creating and Destroying Java Objects: Part 1, the author suggests, "One advantage of static factory methods is that, unlike constructors, they have names." –Joshua Bloch. You may get some ideas from the article.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Although the answer by Bozho is pretty good, I'd really consider FooFactoryFactory even if it may sound silly. It conveys the intent like nothing else.

Alternative names are FooMetaFactory and FooPreFactory.

A possible different naming convention is FooParent and FooGrandParent.

Asclepius
  • 57,944
  • 17
  • 167
  • 143