0

As far my understanding goes while using the factory method we used to get the object by calling a static method of the factory .. like Foo f = Factory.getObj() .. the method is made static as we do not need to create an object before calling the getObj method which will actually return my desired concrete object. But why it is not common to create an instance of the Factory class using new Factory() and then calling the req getObj method? Sorry if this sound foolish but why it is common to make the factory method as static?

Also is static factory and factory are two different thing?

user2720864
  • 8,015
  • 5
  • 48
  • 60
  • Factory methods ensure that only one instance is being referred in the class. In this, I think, the constructor is made as private, thereby not allowing an object to be created of it directly. It needs to get created via public static method like getObj or getInstance etc – MansoorShaikh Aug 27 '13 at 11:56
  • Agreed, the factory constructor is supposed to be private, is it only to ensure that there exists only a single instance of the factory? – user2720864 Aug 27 '13 at 11:58
  • Check those : [Static vs non-static](http://stackoverflow.com/questions/8147163/instance-factory-methods-vs-static-factory-methods) and [Constructor vs factory](http://stackoverflow.com/questions/4617311/creation-of-objects-constructors-or-static-factory-methods) , they should help you understand. And this [one too](http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java) – Jonathan Drapeau Aug 27 '13 at 12:15

2 Answers2

1

You have to ask yourself:

Will different objects of Factory class behave differently? In this case, will they create new objects in a different manner?

The answer is No. If a method is not object scoped, its a perfect candidate for static.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
  • even in case of object creation with some condition i can do some thing like `Foo f = Factory.getObj(String type)` and inside the `getObj` method I can choose which obj to implement based on the type given. So when should i use `new Factory()` ? Sorry if this sound stupid – user2720864 Aug 27 '13 at 12:03
  • If `getObj()`'s definition is same across all instances of Factory class and is not dependent upon instance variables of Factory class. That means all `getObj()` are actually the same and can be replaced by a single static method. – rocketboy Aug 27 '13 at 12:07
  • Do you mean if i have `Factory.getObj()` and Factory.getObj(String type)` then it make sense not to make them static – user2720864 Aug 27 '13 at 12:11
1

You should read Effective Java Item 1: Consider static factory methods instead of constructors. There is a detailed explanation there. Also, classical Factory Method design pattern http://en.wikipedia.org/wiki/Factory_method_pattern is not static. So it can be used both ways.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • so static factory and classical factory method are two different things? – user2720864 Aug 27 '13 at 12:06
  • 1
    yes, Effective Java notes that: Note that a static factory method is not the same as the Factory Method pattern from Design Patterns – Evgeniy Dorofeev Aug 27 '13 at 12:08
  • What should I keep in mind weather to use a static factory or the other – user2720864 Aug 27 '13 at 12:10
  • Depends on situation. Integer.valueOf uses static and nobody doubts its a good idea. But on the other hand using static methods is not OOP, you cannot define an interface for static method, you cannot pass a factory subclass etc – Evgeniy Dorofeev Aug 27 '13 at 12:16
  • So what I found is there is a big advantage of using a static factory, i.e I can return an object of any subtype which will not be possible from a normal constructor call. Is this what you were trying to point out. – user2720864 Aug 27 '13 at 12:18