能否在工厂设计模式中拥有多于1个抽象类型?

huangapple go评论79阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月7日 15:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64239386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定