DDD 书,Eric Evans:“工厂应该抽象为所需的类型,而不是创建的具体类”是什么意思?
在 Eric Evans 所著的《领域驱动设计》一书中,第 6 章“工厂”部分(第 139 页)中写道:
“任何优秀工厂的两个基本要求是:
……
”2。 FACTORY 应该抽象为所需的类型,而不是创建的具体类。”
您能否详细说明一下关于基本要求 2 的陈述的含义。
In the book Domain Driven Design, by Eric Evans, in Chapter 6 in the section on "Factories" (page 139) it says the following:
"The two basic requirements for any good FACTORY are:
...
"2. The FACTORY should be abstracted to the type desired rather than the concrete class(es) created."
Could you please elaborate on what is meant by that statement about basic requirement number 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Carlos Loth 的答案是正确的,但您应该始终记住使用 Abstract Factory ,就像这样将允许您将具体工厂与具体类型耦合,而无需将消费者与具体工厂或类型耦合。
Carlos Loth's answer is correct, but you should always remember to use an Abstract Factory as well, as this will allow you to couple concrete factories to concrete types without coupling consumers to concrete factories or types.
我认为这意味着你永远不应该从工厂返回具体类型。
例如,如果您有一个接口(
ISomething
)、一个抽象类SomethingBase
,最后是一些实现该接口并从基类继承的类。您的创建方法应该返回接口类型,而不是基本类型。我认为这就是这个想法。而不是
I think it means you should never return a concrete type from your factory.
For example, if you have an interface, let's say
ISomething
, an abstract classSomethingBase
and finally some classes that implement this interface and inherit from the base class. Your creation method should return the interface type, instead of the base type. I think that is the idea.Instead of