File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Is this the Factory Method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Is this the Factory Method?" Watch "Is this the Factory Method?" New topic

Is this the Factory Method?

Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 91
Hi all,

There is a kind of method that is used quite often. I will use an example: suppose that we have 2 classes Man and Woman that extend class Human. And in another class HumanUtilities, we have:

Is the getInstance() method a Factory Method? If so, this method does not follow the definition of Factory Method Pattern: "Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."

If this is not a factory method, what is this? I notice that this kind of method is used VERY OFTEN.

Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 91
Hi, can anyone help me?
Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 117
According to your definition, this is not a "factory method". Real "factory method" should use non-static non-final factory method. Even better if you create two classes and override or implement factory method in child.

Anyway, your example is a kind of "factory". And usually just mentioning "factory" will be sufficient in a non-academic environment. You can see concrete factory implementation in a code or in a diagram. You don't need to distinguish different kinds of factory in a high-level design. Hence you don't need detailed classification of "factories". You only need it when you study them (classify, calculate frequencies, etc...). And in this case better consult your teacher or buddies, because terms may vary slightly in different places.

And one more note. Overriding "factory method" in subclasses is usually not a good idea. Using pure interface (and passing an instance in constructor) usually will be more adaptable that having method hardcoded and overriden in some places.
I agree. Here's the link:
subject: Is this the Factory Method?
It's not a secret anymore!