Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes why to use factory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why to use factory?" Watch "why to use factory?" New topic

why to use factory?

Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Hi ranchers, what's the point in using calling like this?

instance = MyFactory.getNewInstance();
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
There are some classes that are defined as abstract, and the only way you can
obtain an instance of the class is to use a factory method.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Factory is used when the implementation of the class is unknown until run time. You usually have an interface, then multiple classes that implement the interface. Here is a practical example I remember from school.Here is the factory code.
Now in you application, you can determine what kind of sort utility you want to use at run time...

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Well if I understood you correctly. Factory is used for instance when an instance is created via relfection?
I agree. Here's the link:
subject: why to use factory?
It's not a secret anymore!