2

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?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
kodjeff1
  • 179
  • 7

2 Answers2

2

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.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • Thank you for your reply. Yes - I've read many SO posts on this topic prior to posting my question. I posted this question particularly as I didn't see anyone addressing this facet of the discussion in particular. As far as IPizzaFactory is concerned, it fits the standard definition of the contract for Abstract Factory (think that we expand this to have multiple concrete implementations) - with one striking difference (which gets at the heart of my question)- typically the contract for Abstract Factory will define creational methods for an entire family of products, rather than one product. – kodjeff1 Aug 01 '16 at 15:33
  • 1
    Perhaps [my answer](http://stackoverflow.com/questions/4209791/design-patterns-abstract-factory-vs-factory-method/38668246#38668246) from the second link above has what you're looking for. – jaco0646 Aug 01 '16 at 15:58
  • 1
    Thanks - nice work :) - between this answer and your linked answer I think I get it: As defined by GoF, an important benefit of Abstract Factory is preserving consistency across a product family. And that benefit is integral to Abstract Factory - if you strip it out, you are now 'something else'. Which can be done, it just isn't Abstract Factory any longer. Is that right? – kodjeff1 Aug 01 '16 at 19:58
1

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.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211