I can\'t think of any reasons why one is better than the other. Compare these two implementations:
public class MyClass
{
public MyClass(string fileName
Sometimes it is important to do bookkeeping and/or resource management for every instance of a class created. As a result, it is important that each construction is managed globally and a static method to do the construction will do that nicely.
Methods that do the construction for you are super useful for:
public class Class
{
public static List<Class> FromFiles(IEnumerable<String> paths)
{
List<Class> instances = new List<Class>();
foreach (string path in paths)
{
instances.Add(new Class() { Path = path });
}
return instances;
}
public string Path { get; set; }
}
...since "normal" constructors cannot do this.
I was researching this very point and came across this question but didn't feel it had been answered fully. However, I did find this handy article - Design Guidelines Update: Factories vs. Constructors - by Krzysztof Cwalina (a a Principal Architect on the .NET Framework). It is worth reading the entire article, but here is a brief summary of the main points:
The most common and consistent way to create an instance of a type is via its constructor. However, sometimes a preferable alternative is to use the Factory pattern.
Do prefer constructors over Factories as they are generally more consistent and convenient than specialized construction mechanisms.
Do implement Factory operations as methods, not properties.
Do return instances as method return values, not as out parameters.
Consider using a Factory if you need more control over the creation patterns of instances.
Consider naming Factory methods by concatenating “Create” and the name of the type being created.
Consider naming Factory types by concatenating the name of type being created and “Factory.”
Do not implement your Factory using a static method if the construction operation must be available for subclasses to specialize.
Do use a Factory for conversion style operations.
Do use a Factory if an operation requires parameter information which feels unnatural to pass to a constructor.
Do not use a Factory in situations where the creation operation will be used as a core scenario for instantiation of a type. In these situations, discoverability and consistency are paramount.
Consider using a constructor instead of a Factory. Only after this thought process should you proceed with the implementation of a Factory.
Factories are also often handy to enable type specialization through polymorphism.
Do use a Factory if API users will be coding to a base class or interface whose implementation is subject to change over time.
Do use Factory methods in cases where a developer might not know which type to construct, for instance when coding against a base type or interface.
Do implement Factory operations as virtual instance methods rather than static if they must support polymorphic extension.
Do use the Singleton pattern for Factories which are instance based so as not to force developers to instantiate a Factory type just to invoke one of its members.
Do use a Factory method if a constructor would be insufficient to describe the operation being performed, and the additional information gained from an individually named Factory makes an operation’s purpose clearer.
Another common case where factory methods help (in languages like C# which don't support type inference from constructor) is when you have to minimize type parameter specification. Consider the common case of Tuple
class. You can either do:
new Tuple<int, int>(1, 1); //constructor call
or
Tuple.Create(1, 1); //factory pattern.
The static Create
method can instantiate and return:
MyClass
instanceMyClass
null
The factory method is often used to hide the exact type of the object being created. An abstract class can have a static factory method, which then selects the concrete derived class to instantiate depending on some condition. Only the abstract base class is publicly documented. This leaves the library supplier some freedom to change the exact type being instantiated without breaking existing code.
An example from the .NET framework is the static factory methods on the Expression
class.