Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Q on method types: what is a factory method?

 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I going through some example code and it mentions
a factory method.

What is the definition of a factory method? Why
is it generally used for?

Cheers,

Gian Franco
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a factory is something that creates other things.
A factory method is a method whose sole purpose is to create Objects of some type.
Usually it will take its input to determine which type that should be and will return a fresh object of that type. Sometimes it may do some initialisation on that object based on the input.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One cool thing about most factory methods is that you don't need to know exactly what kind of object will be returned. Maybe I say

Button b = factory.getButton("Ok");

but depending on user preferences I may get a PlainButton, a RoundButton, a BeveledButton, an Animated4DButton, etc. The factory encapsulates (hides in a single place) the logic to decide what kind of button to make so I don't have to check user preferences all over the place. And the factory can return new button types I've never heard of without changing my code. Of course all buttons have to extend or implement Button so I can work with them.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the way I'm implementing some factories I don't even have to retool the factory to have it create things that didn't exist when the factory was built.

All information required to determine what actual class to create when is contained in configuration settings for the factory (in my case usually a simple properties file listing commands to the factory and associated classes to return instances of, but more complex uses could use XML configurations to describe more complex business rules).
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeroen, Stan,

Do factory methods only relate to the GOF
homonymous pattern and thus 'has to' comply
to its specification?

Cheers,

Gian Franco
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the different between factory method and abstract factory?
Which one is more commonly used?
In EJB, a home object is factory, it means factory method or abstract factory?

Thanks!
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarah

In EJB Home is a factory which produces objects.

This is how I define factory and abstract factory method,
1. factory -- the one that produces objects
2. abstract factory -- the one that produces factory objects

Regards,
Maulin
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A factory that can produce new types without touchign the factory code ... that's pretty neat. I've done a couple command or strategy factories where configuration has a mapping of keywords to fully qualified class names. It's something like this - with more error handling ...

Why get a concrete factory from another factory? The first one decides what kind of factory to make and he second one makes a family of useful things. I used Button as an example above ... what if you wanted a dozen different UI widgets all of the same look and feel?

Ugh, I probably would never call something a factory factory. But does the idea make sense?
 
author
Posts: 33
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit of distinction here... we seem to be discussing two types of factories: what I'd call a "simple factory" and an abstract factory. Only Abstract Factory is a GoF pattern, the Simple Factory isn't. Rather, GoF describes a pattern called Factory Method that defines an interface that relies on subclasses to actually do the creation. Its a subtle difference and little more complex than the typical factory we all know and love.

But so what if its not in GoF, if its useful, use it!

Eric
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely two things. I didn't call out the distinction clearly. Sorry about that.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic