Software Design

Factory Method vs. Abstract Factory

The GoF Factory Method delegates the decision of an object's concrete type to subclasses, while Abstract Factory creates families of related products together. This article compares their intent and selection criteria with Swift examples.

4 min read
Cover image for Factory Method vs. Abstract Factory

When studying design patterns, there’s always a point where you get stuck at least once.

That point is usually Factory Method vs. Abstract Factory. The names are similar, and both “create objects on your behalf,” so they’re easy to confuse.

It’s also a common interview question, so I’ve organized it all in one place here.

Let’s start with the key idea. Factory Method delegates deciding which class to instantiate for a single object to a subclass, while Abstract Factory creates several related objects—a product family—as a unit.

A static factory that names a creation operation, such as static func make, is separate from the GoF Factory Method. If you’re interested in practical API design for this approach, distinguish Swift Static Factory Method first.

One object or one set—that’s the biggest difference.

What Is Factory Method?

Factory Method is a pattern that transfers responsibility for creating objects from a superclass to a subclass.

The superclass only defines that it creates a button; the child class decides which button to create.

The code makes it much quicker to understand.

protocol Button { func render() }

class Dialog {
    // Factory Method: the subclass decides what to create
    func createButton() -> Button { fatalError("Implemented in the subclass") }

    func render() {
        let button = createButton()
        button.render()
    }
}

class IOSDialog: Dialog {
    override func createButton() -> Button { IOSButton() }
}

Dialog doesn’t need to know which button it uses. When a new platform appears, adding one more subclass is enough.

The key is inheritance: subclassing lets you replace the creation point.


How Is Abstract Factory Different?

Abstract Factory is a pattern for creating related objects as a single set.

It handles a group of objects that belong together—such as a button, checkbox, and scrollbar—not just one button.

protocol GUIFactory {
    func createButton() -> Button
    func createCheckbox() -> Checkbox
}

class IOSFactory: GUIFactory {
    func createButton() -> Button { IOSButton() }
    func createCheckbox() -> Checkbox { IOSCheckbox() }
}

class MacFactory: GUIFactory {
    func createButton() -> Button { MacButton() }
    func createCheckbox() -> Checkbox { MacCheckbox() }
}

Consistency is what matters here. With an iOS factory, both the button and checkbox use the iOS style. You won’t accidentally mix a Mac checkbox into an iOS UI.

Abstract Factory is usually used by injecting a factory object from outside. It works through composition, not inheritance.

Swap the factory, and the entire set changes, whether it's iOS or Mac
Swap the factory, and the entire set changes, whether it's iOS or Mac

Factory Method solves “what should we create?” with inheritance,

while Abstract Factory solves “which set should we create?” with composition.


A Side-by-Side Comparison

It gets confusing when explained only in words, so here’s a table.

Category Factory Method Abstract Factory
Purpose Delegate creation of one object Create a family of related objects
Basis Inheritance (subclassing) Composition (assembly)
Creates One product type Multiple products (a product family)
Extension Add a new subclass Add a new factory
Common example createButton() Entire GUIFactory

Interestingly, Abstract Factory is often filled internally with Factory Methods. They aren’t competing patterns; it’s more accurate to see them as patterns that operate at different scales.


When to Use It—and When to Avoid It

The decision criteria in production are simpler than you might think.

  • If you create only one kind of object, Factory Method is enough. Adding a factory interface as well would be overengineering.
  • Consider Abstract Factory when two or more objects need to move together.
  • If the product family is unlikely to grow, a simple conditional branch is often better.
Situation Decision
One product to create Factory Method
Products move together as a set Abstract Factory
Product family grows frequently Abstract Factory
Only 2–3 branches Simple branching without a pattern

Patterns are tools, not goals. Adding abstractions in advance to code that won’t grow only makes it harder to read.

Patterns are only tools, so it's not too late to use one when you actually need it
Patterns are only tools, so it's not too late to use one when you actually need it

How This Appears in Interviews

Q. Please explain the difference between Factory Method and Abstract Factory. A. Factory Method is an inheritance-based pattern that delegates creating one object to a subclass. Abstract Factory is a composition-based pattern that consistently creates a family of related objects. The key difference is whether it creates one object or one set.

Q. Is Factory Method used inside Abstract Factory? A. Yes, often. Each creation method in an Abstract Factory is frequently implemented internally in the form of a Factory Method. They aren’t opposing concepts; they’re patterns with different scopes.

You’re set if seeing either pattern’s name now brings to mind “one object or one set?” Give this article one quick reread before an interview, and you’ll feel prepared.