Abstract Factory vs Factory Method
What you'll learn: How Abstract Factory and Factory Method differ in creating single objects versus coordinated families of related objects.
The Core Difference
Both patterns help you create objects without specifying exact classes, but they solve different problems:
Factory Method creates one type of object at a time. It lets subclasses decide which specific class to instantiate.
Abstract Factory creates families of related objects that are designed to work together. It ensures all created objects are compatible with each other.
A Real-World Analogy
Think of a Factory Method like a single pizza oven that makes one pizza at a time. Different restaurants (subclasses) might use the same oven design but create different styles: New York-style, Chicago deep-dish, or Neapolitan.
An Abstract Factory is like ordering a complete dining set. When you choose "Modern" style, you get a modern table, modern chairs, and modern lighting—all coordinated. Choose "Victorian" and you get a complete Victorian set. The factory ensures everything matches.
When to Use Each
Use Factory Method when:
- You need to create variations of a single product type
- Subclasses should control which concrete class gets instantiated
Use Abstract Factory when:
- You need multiple related objects that must work together
- You want to enforce consistency across a product family
- You might swap entire families at once (like switching UI themes)
Example Distinction
Factory Method: CreateButton() returns different button types.
Abstract Factory: CreateUI() returns a complete, coordinated set—button, textbox, scrollbar—all matching the same theme (Windows, Mac, Linux).
Key Takeaway: Factory Method creates single objects with variations; Abstract Factory creates coordinated families of related objects that belong together.