• Post Reply Bookmark Topic Watch Topic
  • New Topic

Factory classes??  RSS feed

 
madhup narain
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats a Factory class ???
 
Andrew Nomos
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Factory class is a class that creates instances of other classes.
See Factory Pattern by GoF.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a good article on the factory pattern.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "Gang of Four" book talks about factory *method* and abstract factory. These names and descriptions are among the most confusing in the book. The discussion so far fits "factory method". we can leave "abstract factory" for another day.

When you get into larger and more complex systems, factories can offer a lot of freedom for future growth and change. If I code:

Widget w = new Widget();
w.someWidgetMethod();

my code is forever stuck with the class Widget. If I code:

WidgetInterface w = WidgetFactory.getWidget();
w.someWidgetMethod();

my code will work with any new class that implements the interface and we can configure the factory to give me different implementations based on the day of the week or changes in business policy or whatever. It's obviously more work to do this, but it decouples me from any particular Widget which might pay off big in the future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!