Why does Abstract Factory deal with families, and Factory Method with generating a single object?

前端 未结 2 1030
粉色の甜心
粉色の甜心 2020-12-21 12:14

From what I have read, the abstract factory pattern typically concerns itself with creating several objects which are all associated with the same family, and the factory me

2条回答
  •  情歌与酒
    2020-12-21 12:54

    In the given examples, BasePizzaCreator is an Abstract Factory, but IPizzaFactory is not any GoF design pattern (though it is sometimes referred to as a Simple Factory).

    As to why Abstract Factory deals with product families while Factory Method deals with a single product: that's simply how the GoF defined them. The GoF book mentions the most common way to implement an Abstract Factory is with multiple Factory Methods; but I've not seen that in practice. From a client's perspective, Abstract Factory may be preferable because clients invoke it through composition/delegation, as opposed to Factory Method which requires clients to inherit that method.

    Finally, note that "Abstract Factory vs. Factory Method" is the second-most popular design patterns topic on Stack Overflow. Unfortunately, there is a lot of (highly-upvoted) misinformation posted as well, so when in doubt, always refer back to the book.

    • Differences between Abstract Factory Pattern and Factory Method
    • Design Patterns: Abstract Factory vs Factory Method
    • Design Patterns: Factory vs Factory method vs Abstract Factory
    • Factory, Abstract Factory and Factory Method

提交回复
热议问题