wood burning stoves*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Factory Method Pattern: Mental block? 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 "Factory Method Pattern: Mental block?" Watch "Factory Method Pattern: Mental block?" New topic
Author

Factory Method Pattern: Mental block?

Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Hi,
I can't seem to figure this simple pattern out.

From http://en.wikipedia.org/wiki/Factory_method_pattern:
"The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created."

The code below is the Java example of this pattern. To me that code is a Factory class which uses a switch statement to choose the object to be created- where does the 'overriding to specify the derived type of product that will be created' come into it? Is it poorly worded or am I just misreading it?




nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
I don't know whether i have undrstood your question correctly or not. Normally there will be some input to these method and based on that you will instantiate a class. Normally you call a method return type of that will be of interface or abstract class.

For example i have User interface where user wants to upload a jar file or a java class, in my code i got a value based on user selection(which is some radio button or using uploaded content to know what is uploaded) and instantiate my class for further processing.
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
nitin pokhriyal wrote:I don't know whether i have undrstood your question correctly or not. Normally there will be some input to these method and based on that you will instantiate a class. Normally you call a method return type of that will be of interface or abstract class.

For example i have User interface where user wants to upload a jar file or a java class, in my code i got a value based on user selection(which is some radio button or using uploaded content to know what is uploaded) and instantiate my class for further processing.


Hi, thanks for the reply.

I understand the code, I just don't see how it fits the description of a Factory Method Pattern.

Taken from Design Patterns(GoF): Intent of Factory method: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Ok in this case 'the interface for creating an object' is the abstract class Pizza(correct?) and the subclasses would be HamMushroomPizza/DeluxePizza/HawaiianPizza. However the subclasses don't decide anything, it's all done in a switch statement in a completely different class.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The Wikipedia description of the Factory Method pattern is not correct. The objects which are created with the factory method implementation do not implement the factory method.

...'the interface for creating an object' is the abstract class Pizza(correct?)...


No. The PizzaFactory object has the implementation of the interface for creating Pizza objects.

The sub-classes (aka child classes) of Pizza are the implementations. These objects contain their specifc implementation of the common interface getPrice().

The factory method in this example is createPizza(PizzaType x). It isn't the getPrice() method.

In other words, PizzaFactory object has the factory method, not the Pizza objects.

I suggest that you purchase the Design Patterns book instead of relying upon the accuracy and content of "web pages." Keep in mind that the Abstract Factory and the Factory Method patterns are not the same. Make sure that the material you are studying does not mix up the definitions in error.

Hope this helps.
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Frank Bennett wrote:The Wikipedia description of the Factory Method pattern is not correct. The objects which are created with the factory method implementation do not implement the factory method.

...'the interface for creating an object' is the abstract class Pizza(correct?)...


No. The PizzaFactory object has the implementation of the interface for creating Pizza objects.

The sub-classes (aka child classes) of Pizza are the implementations. These objects contain their specifc implementation of the common interface getPrice().

The factory method in this example is createPizza(PizzaType x). It isn't the getPrice() method.

In other words, PizzaFactory object has the factory method, not the Pizza objects.

I suggest that you purchase the Design Patterns book instead of relying upon the accuracy and content of "web pages." Keep in mind that the Abstract Factory and the Factory Method patterns are not the same. Make sure that the material you are studying does not mix up the definitions in error.

Hope this helps.


Sorry, I meant to get around to replying to this...

I have GoF book, but I don't find their explanations very clear, hence why I was on wikipedia!- the book is probably aimed at more experienced programmers.

I certainly think the example on wikipedia is incorrect, for the description that is given, as it doesn't subclass the factory class however it does show highlight the advantages of using a factory method to create an object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Factory Method Pattern: Mental block?
 
Similar Threads
HFDP + factory pattern
Factory Method issue from HeadFirst Design Patterns book
Factory Vs Abstract Factory Pattern
Factory Patter and Abstract Factory Pattern
Doubt in Design Pattern "Factory"