Static factory methods vs Instance (normal) constructors?

前端 未结 11 1457
醉梦人生
醉梦人生 2020-12-02 12:16

In a language where both are available, would you prefer to see an instance constructor or a static method that returns an instance?

For example, if you\'re creating

11条回答
  •  时光取名叫无心
    2020-12-02 12:34

    I personally prefer to see a normal constructor, since contructors should be used to construct. However, if there is a good reason to not use one, ie if FromCharacters explicitly stated that it didn't allocate new memory, it would be worthwhile. The "new" in the invocation has meaning.

提交回复
热议问题