Static factory methods vs Instance (normal) constructors?

前端 未结 11 1456
醉梦人生
醉梦人生 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:39

    If your object is immutable, you may be able to use the static method to return cached objects and save yourself the memory allocation and processing.

提交回复
热议问题