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

落花浮王杯 提交于 2019-11-28 10:54:20

问题


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 method pattern concerns itself with generating a single object.

Consider the following example, which flips those concerns:

// Factory Method (base class) allowing for creation of families of objects
public class BasePizzaCreator{
    abstract ISauce CreateSauce();
    abstract IToppings CreateToppings();
    abstract ICrust CreateCrust();
}

// Abstract Factory (interface) defining contract to create a single object
public interface IPizzaFactory{
    abstract IPizza CreatePizza();
}

It is obvious that you can use them this way - but is it a violation of the spirit of the patterns? If so, why?

What I really want to understand here is this: Why is Abstract Factory the better approach for creating families of related objects, and Factory method the better approach to creating a single object?


回答1:


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



回答2:


That's the difference in intent of these two patterns.

FactoryMethod : Define an interface for creating an object, but let subclasses decide which class to instantiate. FactoryMethod lets a class defer instantiation to subclasses.

AbstractFactory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

AbstractFactory classes are often implemented with FactoryMethods but they can be implemented even with Prototypes

Generally design start with FactoryMethod and may evolve towards other creational patterns like AbstractFactory, Prototype or Builder

I too agree with @jaco0646 answer regarding advantage of using AbstractFactory (uses delegation/composition) from client instead of using FactoryMethod (which based on inheritance)

Regarding your last query:

Why is Abstract Factory the better approach for creating families of related objects, and Factory method the better approach to creating a single object?

Yes. Both have meant for different purpose as quoted in the intent.

AbstractFactory has advantage of returning one of the products from a family.

Advantage of FactoryMethod: It can return the same instance multiple times, or can return a subclass rather than an object of that exact type.

Refer to this sourcemaking link for better understanding of these patterns.



来源:https://stackoverflow.com/questions/38661531/why-does-abstract-factory-deal-with-families-and-factory-method-with-generating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!