This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why to use factory?" Watch "why to use factory?" New topic
Author

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why to use factory?