• 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

Is this the Factory Method?

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, can anyone help me?
 
Ranch Hand
Posts: 121
12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic