英文:
Can a 'Factory' Design Pattern have more than 1 abstract type in a factory?
问题
请注意;工厂模式,而不是抽象工厂模式。
假设有一个名为 Shape
的接口,具有诸如 Pentagon
的具体类。假设一个名为 Quadrilateral
的抽象类实现了 Shape
接口,并且具有自己的具体类型,比如 Square
。
这仍然被视为工厂方法模式吗?
英文:
Please note; Factory, not Abstract Factory.
Say, there is an interface Shape
with concrete classes like Pentagon
. Let's say an abstract class Quadrilateral
implements Shape
and has its own concrete types like Square
.
Is this still considered a Factory Method?
答案1
得分: 1
首先,Shape
接口表示工厂创建的产品。只要工厂返回Shape
实例,产品继承层次的任何其他细节对工厂及其客户端都不相关。
其次,评论中链接的教程描述了《Head First Design Patterns》所称的 简单工厂。这不是 GoF 设计模式。您可以在这里阅读我对 GoF 模式的理解。
最后,警告:谨防使用“工厂模式”一词的任何博客或教程。并不存在名为“Factory”的单一模式。工厂是一类模式,包括 GoF 书中的内部和外部实例。至少有四种常用的工厂模式。
英文:
Firstly, the Shape
interface represents the product created by the factory. So long as the factory returns Shape
instances, any other details of the product inheritance hierarchy are irrelevant to both the factory and its clients.
Secondly, the tutorial linked in the comment describes what Head First Design Patterns calls a Simple Factory. It is not a GoF design pattern. You can read my take on the GoF patterns here.
Finally, a warning: beware any blog or tutorial using the phrase "Factory Pattern". There is no single pattern named "Factory". Factory is a category of patterns including instances both inside and outside the GoF book. There are at least four factory patterns in common use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论