Factory vs instance constructors

后端 未结 10 1930
渐次进展
渐次进展 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条回答
  •  没有蜡笔的小新
    2020-12-13 18:30

    As you can see, myClass is not following a "classic" factory pattern (where the class of the instance is not known/exposed outside the factory).

    However in this case, the .NET framework team may not be aiming for a factory pattern, but I guess they do not want you to just new up the target class directly with a filename via a constructor. A factory might be overkill if only this class is being provided.

    This pattern is sometimes seen in clone() methods, where an object can return an instance which is a kind of duplicate of itself.

    Also maybe although the class is public, they might want to do some checks with the instantiation, filename, etc. and if they implemented a factory, the target class could still be created and invoked, bypassing the checks.

提交回复
热议问题