• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Factory Class??

 
Daniel Sie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've heard of factory methods. But what is a factory class?
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure either but would it be a class that is instantiated only via its factory method(s) ?
Pho
 
John Warland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John!!! It looks similar to an Array List to me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic