Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good name for class which creates factories? (FooFactoryFactory sounds silly imo)

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?

like image 483
Roman Avatar asked Feb 02 '10 18:02

Roman


2 Answers

  • 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
like image 149
Bozho Avatar answered Nov 15 '22 18:11

Bozho


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.

like image 25
trashgod Avatar answered Nov 15 '22 17:11

trashgod