abstract-factory
Produce families of related objects without specifying their concrete classes.
SKILL.mdskills/code-quality/abstract-factory/SKILL.md
GitHubAbstract Factory
Provides an interface for creating families of related objects. Swapping the factory swaps the whole product family consistently — no mixed-family products.
When to Use
- Code must work with multiple families of related products (e.g., UI themes, cloud providers)
- You want to enforce that products from the same family are used together
- You have a set of factory methods and want to group them behind a single interface
How to Apply
- Map out the product types and their variants (families)
- Define abstract interfaces for every product type
- Define the
AbstractFactoryinterface with a creation method for each product type - Implement a concrete factory class for each product family
- Initialize concrete factories in application startup; inject them where needed
Example
interface Button { paint(): void; }
interface Checkbox { paint(): void; }
interface GUIFactory {
createButton(): Button;
createCheckbox(): Checkbox;
}
class WinFactory implements GUIFactory {
createButton() { return new WinButton(); }
createCheckbox() { return new WinCheckbox(); }
}
class MacFactory implements GUIFactory {
createButton() { return new MacButton(); }
createCheckbox() { return new MacCheckbox(); }
}
class App {
constructor(private factory: GUIFactory) {}
build() {
const btn = this.factory.createButton();
btn.paint();
}
}