Factory vs instance constructors

后端 未结 10 1869
渐次进展
渐次进展 2020-12-13 17:41

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         


        
10条回答
  •  -上瘾入骨i
    2020-12-13 18:30

    You can curry factory methods. Trying to curry a constructor gives you: a factory method.

    I use this in some of my code that enumerates the Windows Device Manager tree. One of the classes lists the serial ports, each serial port has a connection factory property which returns a curried factory method with the port address (really ugly PNP device string) stored in internal state while the baud rate, stop bits, parity are all provided later.

提交回复
热议问题