Factory

Deciding which class to instantiate, somewhere the caller doesn't have to know.

Construction is a decision, and new hardcodes it. The moment a caller writes new PayPalGateway() it is welded to that class, to its constructor signature, and to knowing which gateway is right. A factory moves that decision behind a call that returns the interface, so the caller asks for a payment gateway and stops caring which one arrives.

Three things get called factories and it's worth separating them. A *simple factory* is one method with a switch โ€” not a Gang of Four pattern at all, and completely fine. *Factory Method* makes the choice a subclass responsibility: the base class defines the algorithm and defers one construction step to whoever extends it. *Abstract Factory* groups several related constructions so a whole family is chosen at once, which is how you avoid pairing a Windows button with a Mac scrollbar.

The pattern it's confused with is Builder, and the split is about *why* construction is hard. Factory answers "which class?" โ€” one call, several possible types. Builder answers "how many parameters?" โ€” one known type, assembled over several steps. If you're choosing an implementation you want a factory; if you're avoiding a constructor with nine arguments you want a builder.

What they ask, and what to say

What does a factory actually buy you over calling new?

The caller depends on the interface instead of a concrete class, so which implementation is used becomes one decision in one place rather than a fact welded into every call site. That's what makes it swappable and testable.

Why they ask: Tests whether you can justify indirection rather than just recognising it.

Factory Method versus Abstract Factory?

Factory Method defers one product's creation to a subclass โ€” the base class runs the algorithm and asks a hook for the piece it can't make. Abstract Factory creates a whole family together, so you can't accidentally mix a widget from one theme with a widget from another.

Why they ask: The names are close enough that people bluff. Anchoring each to the problem it prevents shows real familiarity.

Factory or Builder โ€” how do you choose?

Ask what's hard about the construction. If it's which type to make, that's a factory. If it's a known type with too many optional parameters to pass positionally, that's a builder. Different problems that both happen to live at construction time.

Why they ask: A very common follow-up, and the honest answer is a one-line rule rather than two definitions.

Where it shows up

Designs on this site that reach for Factory naturally, rather than for decoration.