Abstract Factory and Builder
A factory that produces matching families of related objects, and building one complex object step by step instead of an 8-argument constructor.
Abstract Factory and Builder
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Abstract Factory exists for a problem Factory Method doesn't solve: producing a whole family of related objects that have to stay consistent with each other, like a UI toolkit's Button and Checkbox, which both need to match the same OS theme.
- The shape is one abstract factory interface — say `GUIFactory` with `createButton()` and `createCheckbox()` — implemented once per family, as `WindowsFactory` and `MacFactory`; the calling code picks a concrete factory once and then only ever calls the abstract factory's methods.
- This removes the risk of mismatched families — a `WindowsButton` paired with a `MacCheckbox` — because no call site ever picks button and checkbox types independently; the factory that produces one always produces the other from the same family.
- Builder exists for a different problem: a single complex object with many optional parts, where a constructor would need a long, error-prone, order-sensitive argument list — the 'telescoping constructor' problem.
- A builder exposes one chained method per optional part — `.size(12).addTopping("pepperoni").thinCrust().build()` — so construction happens step by step, with each step named and skippable, and `build()` returns the finished, often immutable, object only at the end.
- Builder's core idea is separating the process of construction from the final representation — the same builder interface could, in principle, assemble a real object or a plain description of one, without the code driving the steps changing at all.
From one object to a matching set
Factory Method hides which concrete class a single call produces. Abstract Factory exists for a problem one level up: producing several related objects that all have to agree with each other. The clearest example is a cross-platform UI toolkit. A Button and a Checkbox both need a Windows-styled version and a Mac-styled version, and the two widgets on screen at the same time have to come from the same theme — a Windows-flat button next to a Mac-rounded checkbox looks like a bug, because it is one.
Abstract Factory: one factory per consistent family
Without the pattern, the calling code decides each widget’s type independently: if (os == "mac") { button = new MacButton(); checkbox = new MacCheckbox(); }, repeated at every place in the app that creates a widget. Every one of those call sites is a chance for the branches to drift out of sync — one gets updated for a new theme, another doesn’t.
Abstract Factory collects the related creation methods into one interface: GUIFactory, with createButton() and createCheckbox(). WindowsFactory and MacFactory each implement it, producing their own matching pair. The application picks a concrete factory exactly once — often at startup, based on the detected OS — and from then on only ever calls factory.createButton() and factory.createCheckbox(). It’s structurally impossible for those two calls to return mismatched families, because whichever factory got chosen produces both halves of the pair. Adding a theme means writing one new factory class, not hunting down every place a widget gets created.
Builder: construction as a sequence of steps, not one call
A different problem shows up when a single object has many optional parts. A Pizza constructor that takes size, crust, sauce, cheese, pepperoni, mushrooms, olives, extraCheese is the “telescoping constructor”: callers have to remember argument order, most calls pass several defaults just to reach the one parameter they actually care about, and a two-boolean mix-up (extraCheese=true, thinCrust=false swapped) compiles cleanly and fails silently.
Builder replaces the one giant call with a chain of smaller ones: new PizzaBuilder().size(12).sauce("marinara").addTopping("pepperoni").thinCrust().build(). Each method sets one part and returns the builder itself, so calls read in order, skipping any step that doesn’t apply produces no dangling arguments, and build() only assembles and returns the final Pizza once every step has run. A Director class can sit on top and encode a named recipe — buildMargherita(builder) — as a fixed sequence of builder calls, so common configurations don’t have to be retyped at every call site.
The deeper idea is separating how an object gets built from what the finished thing looks like. The builder owns the step-by-step process; the product it hands back at the end is what actually gets used. That separation is what lets the same sequence of builder calls, in principle, produce genuinely different representations — an in-memory Pizza object in one implementation, a plain order-ticket string in another — without the code driving the steps caring which one it’s getting.
Where the indirection stops being worth it
Abstract Factory only pays for itself when the families actually have to co-vary — if an app only ever targets one platform, or a “related” set of objects never actually needs to change together, the factory-of-factories is a layer with nothing to protect. Builder has the same shape of cost: an object with two or three required fields and no real optional variation doesn’t need a separate builder class standing between the caller and a constructor that would have worked fine on its own. Both patterns solve a real problem — mismatched families, telescoping constructors — and both are overkill the moment that problem isn’t actually present.
Check yourself
Why would an Abstract Factory, not just two separate Factory Methods, be the right fix for a UI toolkit that supports both Windows and Mac themes?
Two independent factory methods for buttons and checkboxes would let a call site accidentally request a Windows button and a Mac checkbox on the same screen; Abstract Factory bundles both creation methods behind one factory per theme, so picking the factory once guarantees every widget it produces belongs to the same consistent family.
What specific problem does Builder solve that a constructor with named optional parameters doesn't fully fix?
Even with named parameters, a single call still has to supply every value for one complex object in one shot, and any validation or multi-step assembly logic (like skipping a step entirely, or enforcing that one step requires another) has nowhere to live; a builder spreads construction across separate method calls, each able to validate or depend on the step before it.
When is reaching for Builder overkill?
When the object has two or three required fields and no meaningful optional variation — a plain constructor is more direct, and standing up a separate builder class just to call `.build()` once adds a layer of indirection the object never needed.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.