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:
- Is there something special with
Create
methods or it is just a convention (like java:getInstance
)? - What are the good practices in C# regarding factory methods vs constructors? Why, for example, there are several
Create
methods that acceptsXmlWriterSettings
arguments inXmlWriter
and no constructor inXmlTextWriter
with the same purpose? And, in the other hand, why only constructors acceptsEncoding
arguments? - I guess the main question is, in idiomatic C#, when is it recommended to expose factory methods and when should public constructors be exposed?