This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Is this the Factory Method?" Watch "Is this the Factory Method?" New topic
Author

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.

Thanks.
Swerrgy Smith
Ranch Hand

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

Joined: Jun 14, 2013
Posts: 101
    
  10
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.
 
jQuery in Action, 2nd edition
 
subject: Is this the Factory Method?