Is it OK for a factory method to return null?

前端 未结 4 1044
余生分开走
余生分开走 2021-02-12 03:39

I\'m wondering about best practice here. Is it good practice for a factory method to return null if it can\'t create anything? Here\'s an example:

ICommand comma         


        
4条回答
  •  眼角桃花
    2021-02-12 04:01

    I agree with Jon Skeet. CreateCommand clearly implies construction.

    If you won't throw an Exception, then in that scenario I would personally go with the NullCommand implementation, to avoid conditional statements in all consumers and possible NullReferenceException errors.

提交回复
热议问题