“Singleton” factories, ok or bad?

后端 未结 6 775
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 08:55

I\'ve a lot of (abstract) factories and they\'re usually implemented as singletons.

Usually for the convenience of not having to pass them through layers who really hav

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 09:28

    The bad form of singleton is the one that implements the equivalent of your Factory method with:

    return new CalculationFactory ();
    

    That's much harder to stub, mock or wrap.

    The version that returns an object created elsewhere is much better, although like most anything, can be misused or overused.

提交回复
热议问题