views:

227

answers:

7

In Karl Seguin's Foundations of Programming there is a small section on using the factory pattern. He closes the passage by stating "you can accomplish the same functionality with constructor overloading", but doesn't indicate when or why?

So,when does it make more sense to use the factory pattern rather than an overloaded constructor to instantiate an object?

+1  A: 

I use a factory when I want the factory to construct one of several different possible subclasses (and I want the caller to know about the base class but not to know about the subclasses).


Also, occasionally I'll use static methods of the class instead of an overloaded constructor, when the different static methods take the same parameter types (and therefore constructors can't be overloaded based on the parameter type alone). Here's a contrived example:

Department
{
  //static factory methods
  public static Department createFromBoss(string bossName) { ... }
  public static Department createFromLocation(string locationName) { ... }
}
ChrisW
+4  A: 

If you want to have looser coupling then the factory makes more sense, as you can then just call the car factory, pass in the suv enum, and the correct class is returned. Your application doesn't care which class was actually returned, as long as it meets your needs.

James Black
An object that accepts a factory as a argument is also responsible for calling the creation method.
JaredCacurak
A: 

I have 1 situation in which a factory is useful. I have to instantiate a video effect to run on a video. Depending on the type of video, the video effect has a different concrete class.

If i instantiate the concrete class, then i lose the ability to add more video effects later without modifying the instantiation code.

When i add more video effects, i only have to modify the factory to choose the proper concrete class.

Does this make sense ?

Andrew Keith
A: 

It may be that the factory sometimes generates subtypes of the type being returned. You couldn't do this with a constructor, but have the flexibility with a factory.

Frank Schwieterman
+4  A: 

If you're doing Dependency Injection but need to create instances of the dependency as needed within the dependant, one option is to inject an interface to a class factory. The factory can return an interface or an abstract class. This provides flexibility, testability, and decoupling at the cost of some complexity.

TrueWill
Indeed Virtual Construction cannot be achieved without a kind of Factory / Builder on some languages (C++)
Matthieu M.
A: 

I disagree with his statement. Different constructor are used when you have different methods of constructing/initialising the constructor. The Factory Pattern is for use when the initialisation criteria result in you have different objects to construct.

Martin Spamer
A: 

It also makes more sense to use the factory pattern to use sub-classes, as ChrisW illustrated, if you want to implement polymorphism through your methods. If

Department b = Department.createFromBoss();
Department l = Department.createFromLocation();

both return different subclasses of Department then,

b.Close()

and

l.Close()

for example could take different actions, which would be messier if you had to try and Close() a single object via constructor overloading.

Rafe Lavelle