• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Factory Method Pattern: Mental block?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?




 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic