Factory vs instance constructors

Posted by Neil N on Stack Overflow See other posts from Stack Overflow or by Neil N
Published on 2010-06-02T17:06:15Z Indexed on 2010/06/02 17:54 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

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)
    {
        // some code...
    }
}

as opposed to:

public class MyClass
{
    private myClass(){}

    public static Create(string fileName)
    {
       // some code...
    }
}

There are some places in the .Net framework that use the static method to create instances. At first I was thinking, it registers it's instances to keep track of them, but regular constructors could do the same thing through the use of private static variables.

What is the reasoning behind this style?

© Stack Overflow or respective owner

Related posts about c#

Related posts about oop