2

There are two main differences between the design patterns Factory Method and Abstract Factory.

Difference 1 is that Factory Method is mainly based on inheritance. A class in a way uses it's subclass to create objects. The objects created depend on the subclass used. While Abstract Factory is based on inheritance but in a way also on composition - A client 'owns' an Abstract Factory instance to hold a concrete factory.

Difference 2 is that Factory Method creates one object, while Abstract Factory creates a family of related objects.

Difference 1 I understand (although I'm having trouble understanding why anyone would prefer to subclass a class that needs a factory instead of just 'giving' it a factory using composition [unless that class already has a subclass]).

But difference 2 I don't understand.

Why can't Factory Method create a family of products instead of one product? It can certainly return an array of products of the same family. All of these products share a common interface - the method can return an array of that type. Why is it said that this method only produces one product?

Additionally, a factory method inside a concrete subclass of an abstract factory creates different products using different methods that are called by the client.

Why can't a concrete factory in the Factory Method pattern do the same? Have different factory methods for different products?

2

1 Answer 1

1

You need to understand that the Factory Method refers to the functionality of a factory, whereas the Abstract Factory refers to the functionality of a factory of factories. The answer linked by @pdr further expands on this point.

To directly address your main concern,

Difference 2 is that Factory Method creates one object, while Abstract Factory creates a family of related objects.

The Factory Method returns a single implementation for the required functionality, i.e. it returns a FinancialManager or a OperationsManager to satisfy the need for an IManager.

An Abstract Factory will return a factory that then provides you with actual implementations, i.e. it returns a FinancialFactory or an OperationsFactory as the implementation for an IProfessionalFactory. Each of these implement their own version of BuildManager() -> IManager; however, they could also implement other methods to build a family of related objects, like BuildEngineer() -> IEngineer or BuildAssistant() -> IAssistant, each returning a financial- or operations-related implementation based on whether you use a FinancialFactory or an OperationsFactory.

Not the answer you're looking for? Browse other questions tagged or ask your own question.