Repaso
EspaƱol
Design & PracticesIntermediateDP-42

What is the difference between Factory Method and Abstract Factory?

Options

  1. There is no difference between the two, only the name changes
  2. Factory Method makes one product; Abstract Factory makes families of products (Correct option)
  3. Factory Method is meant for threads; Abstract Factory for databases
  4. Abstract Factory has no interfaces and works only with classes

Answer

Factory Method: one method that decides which single product class to create. Abstract Factory: an interface to create whole families of related objects that belong together

Why

Factory Method: a method returns an object of some interface and hides which concrete class is created: NotificationFactory.create("email") gives an EmailNotifier.

Abstract Factory: an object that creates several related products that must be consistent. Example: a UiFactory that creates Button, Checkbox and Menu for either a Windows or a Mac look; you switch the whole family by switching the factory.

In Spring, the container itself acts as a giant factory.

Remember

Factory Method = one product. Abstract Factory = family of products.

Key terms

Practice this question in Repaso