Abstract Factory Pattern
Create related products as a family instead of repeating concrete implementation choices in every screen.
· Last updated: 2026-11-07 · 8 min read
Overview
Consider a dialog whose controls should share a theme. A light-themed button paired with a dark-themed checkbox leaves the dialog looking off. A better single button will not solve it; the dialog needs a rule for choosing parts from one family. Today each screen picks both parts itself, and that choice repeats.
Abstract Factory gathers the creation contract for a family of related products in one place, and the screen then tells its factory which parts it needs instead of picking concrete button classes. The factory is the object that chooses the products on the screen's behalf.
UML
Dialog requests a button and a checkbox from IWidgetFactory. LightFactory creates the light-themed pair, and the screen uses each product through Draw(). IWidgetFactory promises only that both products get made.
History
GoF cataloged Abstract Factory as a creational pattern in Design Patterns, published in 1994. Supporting different user-interface families is one of its motivating situations.1
Late-1980s GUI frameworks separated applications from window-system details through objects. Weinand, Gamma, and Marty's ET++ paper in 1988 describes an abstract interface for window-system resources and drawing.2 Linton, Vlissides, and Calder's 1989 InterViews paper explains how to compose interfaces from objects such as buttons and scroll bars.3
Why use it?
Once every screen inspects the theme and picks its own button and checkbox, the same selection code spreads through the codebase. Decide the factory once, when the screen is assembled, and the screen is left with just the parts it uses. Knowledge of the family then sits in one place: where the screen gets wired up.
Conversely, adding a new product kind means changing the factory contract and every family implementation. The pattern therefore suits a codebase where families change often but the set of product kinds within a family stays stable. A product kind here is an item the factory turns out, such as a button or a checkbox.
Example
The example assembles one dialog from the light factory; its products return names instead of drawing real GUI controls, so the resulting combination is visible. The next example keeps that dialog and swaps only the factory.
using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(new Dialog(new LightFactory()).Render());
interface IButton { string Draw(); }
interface ICheckbox { string Draw(); }
// Keep theme selection out of the dialog and behind a product-family contract.
interface IWidgetFactory
{
IButton Button();
ICheckbox Checkbox();
}
sealed class LightButton : IButton { public string Draw() => "light button"; }
sealed class LightCheckbox : ICheckbox { public string Draw() => "light checkbox"; }
sealed class DarkButton : IButton { public string Draw() => "dark button"; }
sealed class DarkCheckbox : ICheckbox { public string Draw() => "dark checkbox"; }
sealed class LightFactory : IWidgetFactory
{
public IButton Button() => new LightButton();
public ICheckbox Checkbox() => new LightCheckbox();
}
sealed class DarkFactory : IWidgetFactory
{
public IButton Button() => new DarkButton();
public ICheckbox Checkbox() => new DarkCheckbox();
}
// The dialog uses selected products without constructing concrete theme classes.
sealed class Dialog(IWidgetFactory factory)
{
public string Render() => factory.Button().Draw() + " + " + factory.Checkbox().Draw();
}
light button + light checkboxDialog never constructs LightButton directly; the factory picks the concrete button and checkbox. Swap the family and Dialog is unchanged.
An extended example
Pass two factories to the same Dialog in turn; the screen code stays untouched and only the family of created products changes. The one moving part is where the factory gets chosen.
using System;
using System.Collections.Generic;
using System.Linq;
foreach (IWidgetFactory factory in new IWidgetFactory[] { new LightFactory(), new DarkFactory() })
Console.WriteLine(new Dialog(factory).Render());
interface IButton { string Draw(); }
interface ICheckbox { string Draw(); }
// Keep theme selection out of the dialog and behind a product-family contract.
interface IWidgetFactory
{
IButton Button();
ICheckbox Checkbox();
}
sealed class LightButton : IButton { public string Draw() => "light button"; }
sealed class LightCheckbox : ICheckbox { public string Draw() => "light checkbox"; }
sealed class DarkButton : IButton { public string Draw() => "dark button"; }
sealed class DarkCheckbox : ICheckbox { public string Draw() => "dark checkbox"; }
sealed class LightFactory : IWidgetFactory
{
public IButton Button() => new LightButton();
public ICheckbox Checkbox() => new LightCheckbox();
}
sealed class DarkFactory : IWidgetFactory
{
public IButton Button() => new DarkButton();
public ICheckbox Checkbox() => new DarkCheckbox();
}
// The dialog uses selected products without constructing concrete theme classes.
sealed class Dialog(IWidgetFactory factory)
{
public string Render() => factory.Button().Draw() + " + " + factory.Checkbox().Draw();
}
light button + light checkbox
dark button + dark checkboxDarkFactory implements the same creation contract, so Dialog handles both products the same way. Keeping the themes from mixing remains the responsibility of each factory implementation; the interface name alone does not automatically catch products that have been mixed across themes.
TypeScript example
If Dialog takes on choosing the theme as well as drawing each part, the dialog code changes every time a new theme is added. IWidgetFactory picks a related set of products, and Dialog only uses the parts it is handed.
interface IButton { draw(): string; }
interface ICheckbox { draw(): string; }
// Keep theme selection out of the dialog and behind a product-family contract.
interface IWidgetFactory {
button(): IButton;
checkbox(): ICheckbox;
}
class LightButton implements IButton { draw(): string { return "light button"; } }
class LightCheckbox implements ICheckbox { draw(): string { return "light checkbox"; } }
class DarkButton implements IButton { draw(): string { return "dark button"; } }
class DarkCheckbox implements ICheckbox { draw(): string { return "dark checkbox"; } }
class LightFactory implements IWidgetFactory {
button(): IButton { return new LightButton(); }
checkbox(): ICheckbox { return new LightCheckbox(); }
}
class DarkFactory implements IWidgetFactory {
button(): IButton { return new DarkButton(); }
checkbox(): ICheckbox { return new DarkCheckbox(); }
}
// The dialog uses selected products without constructing concrete theme classes.
class Dialog {
constructor(private readonly factory: IWidgetFactory) {}
render(): string {
return this.factory.button().draw() + " + " + this.factory.checkbox().draw();
}
}
for (const factory of [new LightFactory(), new DarkFactory()]) {
console.log(new Dialog(factory).render());
}light button + light checkbox
dark button + dark checkboxThe interfaces that played the same role in C# were ported to TypeScript, and keeping themes from mixing stays the responsibility of each factory implementation.
Structural contrast with Factory Method and asymmetric extensibility
Although their names sound alike, Factory Method Pattern and Abstract Factory rely on different mechanisms and solve different problems. Factory Method uses Inheritance to let a subclass override a single creation step inside a shared algorithm. Abstract Factory uses Composition: it packages a suite of creation methods for an entire product family into a separate object injected into the client (even though each individual method on the concrete factory is often implemented as a factory method).
Keep the asymmetry of change in mind when designing the factory interface. Adding a brand-new product family—say, a HighContrastFactory—is effortless and leaves existing classes untouched. Adding a new kind of product—say, CreateSlider()—forces you to modify IWidgetFactory and every concrete factory implementation in the codebase. In modern applications wired with Dependency injection, reach for an Abstract Factory when multiple products must be switched together as a consistent family or created on demand at runtime, rather than wrapping every single service in a boilerplate factory.
Related reading
Footnotes
- Gamma, Helm, Johnson, Vlissides. Design Patterns (1994). Publisher record. Creational patterns, Abstract Factory. ↩
- André Weinand, Erich Gamma, Rudolf Marty, "ET++ — An Object-Oriented Application Framework in C++," OOPSLA 1988, pp.46–57, especially p.50, Fig.4. DOI. ↩
- Mark A. Linton, John M. Vlissides, and Paul R. Calder, "Composing User Interfaces with InterViews," IEEE Computer 22(2), 1989, pp. 8–22. DOI. ↩