使用工厂而不是构造函数创建对象的阈值是多少? [英] What is your threshold to use factory instead of a constructor to create an object?
本文介绍了使用工厂而不是构造函数创建对象的阈值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 您始终使用工厂。 您使用工厂而不是构造函数创建对象的阈值是多少?
- 只有在您检查null时,您才能使用工厂。
- 您始终使用构造函数
- 您很少使用工厂...这些情况是什么?
利弊
更新:我在项目中从域驱动设计应用工厂模式。创建工厂背后的原因之一是减少域模型中的噪音。
谢谢
解决方案
如果我有一个抽象基类(或接口)和几个具体的派生类,并且有一些逻辑根据哪一个具体类被创建。我在工厂里实现这个逻辑。
What is your threshold to use factory instead of a constructor to create an object?
- You always use factory.
- You use factories only if you have invariant checks other than checking for nulls.
- You always use constructors
- You rarely use factories... what are those cases??
pros and cons
Update: I am applying factory pattern from Domain Driven Design in my project. And one of the reason behind creating factories is to reduce noise in the domain model.
Thanks
解决方案
I use a factory if I have an abstract base class (or an interface), and several concrete derived classes, and there is some logic according to which one of concrete classes is created. I implement that logic in the factory.
这篇关于使用工厂而不是构造函数创建对象的阈值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文