-1

Some of the Java libraries I have used so far seem to have a Factory for seemingly every other class in the library. Apache's FTP Server project, for example, provides a UserManagerFactory class which allows for configuring a prototypical instance of the UserManager class and creating copies of said instance - at least I assume / observe this behaviour. However, wouldn't the Builder or Prototype pattern be more suitable for this use-case? I have read the design patterns book by the GoF and I am pretty sure you use factories if you need to switch between families of related class hierarchies (possibly at runtime).

In projects that do not focus on Java exclusively, such as Google APIs, I see a lot more builder classes and I was wondering whether there was any correlation.

The question arising from my observations is: Do I confuse those patterns, do I not fully understand the libraries' class structure or is the naming in certain libraries really off at times?

  • 1
    There are far too many Java libs with factory classes out there to give you a sensible answer other than "yes to all of the three questions in your last sentence" ;-) If you don't want your question to be closed as "too broad", try to narrow its focus down to a specific case (but avoid also asking out community which can be only answered by the authors of a lib, such questions should be adressed to the authors directly, for example by using their forums/mailing lists). – Doc Brown Nov 19 '18 at 12:03
  • FWIW, having a short look into the UserManagerFactory source code: that looks clearly like an example of the Abstract Factory pattern to me. UserManagerFactory is only an interface, an implementation might use something like the prototype pattern internally (or not, I don't know), that would not be a contradiction. – Doc Brown Nov 19 '18 at 12:16
  • To answer this question well, you'd ultimately have to know how they'd use the factory, but from the library user perspective, giving you a means to establish how a UserManager is created is ideal for extending. In other words, it does everything it should from your perspective, and if I had to guess, an abstract factory pattern suits their needs as well. But again, to know this you'd have to see how they use it. – Neil Nov 19 '18 at 12:34
3

Do I confuse those patterns, do I not fully understand the libraries' class structure or is the naming in certain libraries really off at times?

The names of some of the patterns are unfortunately confusingly similar.

The name UserManagerFactory doesn't tell me if they're using the Factory Method pattern or the Abstract Factory pattern.

The name StringBuilder doesn't tell me if they're using GoF's Builder pattern or Joshua Bloch's Builder pattern.

All that these names give you is a hint. Few faithfully name their classes after the patterns used inside. Since that's an implementation detail they really shouldn't anyway. The name should reflect the abstraction. Not exactly how the magic is done.

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.