5

As a "javaland" programmer I'm used to Factory Methods and Multiple Constructors. My main uses for factory patterns are to delay decisions until runtime, perform some kind of side effect or restriction during the instantiation or hide the concrete type. As I dig into C# I see more and more APIs that mix multiple constructor and static Create methods.

E.g., XmlReader and XmlTextReader. XmlWriter and XmlTextWriter.

My questions are:

  1. Is there something special with Create methods or it is just a convention (like java: getInstance)?
  2. What are the good practices in C# regarding factory methods vs constructors? Why, for example, there are several Create methods that accepts XmlWriterSettings arguments in XmlWriter and no constructor in XmlTextWriter with the same purpose? And, in the other hand, why only constructors accepts Encoding arguments?
  3. I guess the main question is, in idiomatic C#, when is it recommended to expose factory methods and when should public constructors be exposed?
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • I don't think this is particularly a C# question - it's an OO question first and foremost. That being the case, it's at least a partial dupe of [this one](http://stackoverflow.com/q/628950/27615). – razlebe Sep 05 '11 at 13:37
  • I guess I wanted "idiomatic c# opinions", but otherwise you are right, this is a generic question. I just think that programmers from different languages and cultures may have different insights on this. – Anthony Accioly Sep 05 '11 at 13:45

2 Answers2

3

1) The only difference I know of is that generic type inference is supported for methods but not for constructors. Therefore, you can write:

var str = "foo";
var num = 42;
var tuple = Tuple.Create(str, num);

While if you wanted to use a constructor (unfortunately Tuple does not have a public constructor, so the example won't compile) you would have to write:

var tuple = new Tuple<string, int>(str, num);

Of course this only applies to constructors of generic types, so it's not exactly a mainstream difference.

2) I suspect that was kind of a "random" (if "random" sounds harsh, then let's say "not made with best practices foremost in mind") decision, but I don't have any data to back this up.

3) Factory methods are also the only way you can return an object cast as one of its ancestor types. Sometimes this is not only desirable but also necessary because the user of your type might not know what type they are supposed to construct. For example, consider a factory method that returns a Stream which might be either a RedStream or a BlueStream, depending on some input parameters.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
0

Is there something special with Create methods or it is just a convention

It is just a convention on how to name a factory method.

What are the good practices in C# regarding factory methods vs constructors?

Some as java, remember that the .net framework is now showing it’s age and nothing is every removed. So the framework it's self does not always show what is considered to be current good practices.

I guess the main question is, in idiomatic C#, when is it recommended to expose factory methods and when should public constructors be exposed?

Now that is a good question, I think the answer is the same in C# as Java

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317