Why should the getInstance() method in Factory pattern be static?
Asked Answered
M

4

8

In most of the factory pattern implementations, the getInstance method is usually declared as static. The main advantage of factory pattern is to hide the implementation details, but why does getInstance() method needs to be static? Is instantiating a new Factory Object a bad practice?

XYZFactory factory = new XYZFactory(); 
XYZObj obj = factory.getInstance(TYPE);

Vs

XYZObj obj = XYZFactory.getInstance(TYPE);
Melisenda answered 12/9, 2011 at 20:36 Comment(0)
H
4

Daves answer is absolutely correct if the factory method is in the class itself. For a factory method in some other class, I think it is a matter of style. I would go back to the basic question of when is something static: does this method provide behavior to the class as a whole, or to specific instances of the class? I argue factory methods typically offer class-level behavior.

Hultin answered 12/9, 2011 at 20:44 Comment(1)
Wade is absolutely correct, too. I posted only half of my answer to be first ;) Now that I have completed it, I hope it covers all cases...Beauteous
B
10

A lot of factory methods are used to offer an instance of the class itself, without the class exporting any constructors (see e.g. Josh Bloch item 1). If the factory method were an instance method, you wouldn't have an object of the class to start with.

Furthermore, getInstance() is usually independent of any existing instance, so it should be declared static. If it depends on one, a prototype (i.e. clone()) is often preferred.

Finally, you should distinguish between factory method public static getInstance() and an abstract factory, which is a class that hides implementation details often for several interfaces. You must, of course, be able to instantiate subclasses of the abstract factory. You can find a great introduction to creational patterns (Abstract Factory, Factory Method, Prototype, amongst others) in the classic Design Patterns book from the Gang of Four. It also gives an example of a non-static factory method intermixed with a prototype. So you see, many variants are possible...

Beauteous answered 12/9, 2011 at 20:40 Comment(0)
H
4

Daves answer is absolutely correct if the factory method is in the class itself. For a factory method in some other class, I think it is a matter of style. I would go back to the basic question of when is something static: does this method provide behavior to the class as a whole, or to specific instances of the class? I argue factory methods typically offer class-level behavior.

Hultin answered 12/9, 2011 at 20:44 Comment(1)
Wade is absolutely correct, too. I posted only half of my answer to be first ;) Now that I have completed it, I hope it covers all cases...Beauteous
S
0

Factory should give you instances of products, not instances of itself. It's rather like Singleton.

Sluggard answered 12/9, 2011 at 20:44 Comment(0)
F
0

Because you use a factory to create objects. What you need is object instances not factory instances. So you usually look for the simplest, cleanest way of doing it.

It isn't useful to have more instances of the factory laying around. The factory instances will be useless after creating the object(s), so why create useless factory objects when just one long lived instance will do?

Flyleaf answered 12/9, 2011 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.