File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Factory Class?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Factory Class??" Watch "Factory Class??" New topic

Factory Class??

Daniel Sie

Joined: Aug 13, 2001
Posts: 7
I've heard of factory methods. But what is a factory class?
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 782

I'm not sure either but would it be a class that is instantiated only via its factory method(s) ?


John Warland

Joined: Jul 11, 2001
Posts: 3
The idea of a factory class is that it is a class which will create an object for you when you request one.
They are most useful when you want to create lots of similar objects.
so for example, normally you would type
myObject1 = new Object(10, 1);
myObject2 = new Object(10, 2);
myObject3 = new Object(10, 3);
to create series of objects which are similar.
Instead you could just call a method in your factory class like:
myObject1 = myFactClass.giveMeAnother(1);
myObject2 = myFactClass.giveMeAnother(2);
and method giveMeAnother would be something like
public Object giveMeAnother(int val) {
Object newObject = new Object(10, val);
return newObject;
Basically the idea is to give you more control over the creation of your objects. So for example if you wanted to change the default value 10 to something else then you would only have to do it in one place.
Daniel Sie

Joined: Aug 13, 2001
Posts: 7
Thanks John!!! It looks similar to an Array List to me.
I agree. Here's the link:
subject: Factory Class??
It's not a secret anymore!