instance factory methods Vs Static factory methods

前端 未结 5 854
难免孤独
难免孤独 2020-12-23 11:35

Can\'t all factory methods be static ? Does something that produces a product need state ? When is it appropriate to go for a instance factory or static factory method ? Ca

5条回答
  •  悲&欢浪女
    2020-12-23 12:16

    My current preference is to make factory methods not static for the sake of easier testing. You can't change a static factory method call at runtime, whereas if I could provide a factory implementation to the object then I could test it more thoroughly as I'm in more control of the context and object graph.

提交回复
热议问题