Links

Java QA: What is Factory Method in java ?

thanassis | Java Questions And Answers | Wednesday, January 3rd, 2007

The term “Factory Method” refers to a method that is part of the factory design pattern. The factory design pattern suggests that when we want to decouple the instantiations of objects from their type we can defer the instantiations to a class method (static method).

For example, rather than instantiating:

Fruit myFruit = new Apple();

we call a method:

Fruit myFruit = FruitFactory.getApple();

This of course presumes writing the FruitFactory class with its methods.

Initially the factory pattern may seem like a simple transformation but it has huge implications considering scaling. If before you had a system with 1000s of places where you instantiated an appleFruit and you needed to make a change to the instantiation parameters, then you would also had to make changes to all these 1000s places. But if you had chosen to use a “factory method” instead of direct instantiations, you would had only needed to go to the factory method’s implementation and make a single change.
For more information on the factory method look at the wickipedia page:
http://en.wikipedia.org/wiki/factory_design_pattern

For java code on a further variation of the factory idea look at
http://www.thanassis.com/java/java-advanced_day1

No Comments

No comments yet.

Leave a comment

RSS feed for comments on this post. TrackBack URI