ABSTRACT FACTORY — ASSEMBLY LINE GUIDE
Software Design & Architecture · Chapter 06, L01 · Creational Patterns

The Abstract Factory
builds whole families of things —
without the client ever picking parts.

A rebuilt, condensed version of your lecture deck: one story, one running metaphor (a literal factory floor), and memory hooks at every stop so the ideas stick before the exam does.

STATION 01

What is a Design Pattern?

A design pattern is a reusable solution to a recurring object‑oriented design problem in a given context — a template, not finished code. It is not the same as an architectural pattern/style: architecture is about the shape of the whole system; a design pattern solves one recurring problem inside the detailed design.

Memory hook "Pattern = Proven Plan, not Product."

You reuse the plan (structure/roles), you still write the actual classes yourself.

STATION 02

Classifying Patterns — Purpose × Scope

By Purpose — what it does

Type Deals with…
Creational How objects get created
Structural How objects/classes are composed into bigger structures
Behavioral How objects interact and share responsibility

By Scope — when it applies

Scope Applies to…
Class‑level Classes, fixed at design/compile time (e.g., Factory Method)
Object‑level Objects, flexible at run time (e.g., Abstract Factory)
Memory hook "CSB": Creational / Structural / Behavioral = Create it, Stack it, Behave with it.

And: Class = Compile‑time · Object = happens while Operating (runtime).

STATION 03

Documenting a Pattern (just know the idea)

Full pattern docs list categories like IntentMotivationApplicabilityStructureParticipantsCollaborationsConsequencesImplementationSample CodeKnown UsesRelated Patterns. For this course, you only need the gist: what problem it solves, who the players are, and how they collaborate — not the full formal write‑up.

STATION 04

The 5 Creational Patterns

Creational patterns abstract and control how objects are created, hiding the concrete classes behind a common creational interface.

Abstract Factory
families of related objects
Factory Method
one product, subclass decides
Builder
build complex object step‑by‑step
Prototype
clone an existing object
Singleton
exactly one instance, ever
Memory hook "Some People Find Amazing Builders" → S‑P‑F‑A‑B

Singleton, Prototype, Factory Method, Abstract Factory, Builder.

STATION 05

Abstract Factory — the Core Idea

Intent: provide an interface for creating families of related objects — without the client ever naming their concrete classes.

It's an object‑level, creational pattern, made of two class hierarchies working as a pair:

Creator classes
the Abstract Factory + its Concrete Factories — one factory per family
Product classes
the Abstract Products + their Concrete Products — the parts a family produces
The 7‑letter memory hook F·A·C·T·O·R·Y

Families of related products  ·  Abstract interfaces define what's buildable  ·  Client never sees the concrete class  ·  Themes/groups stay internally consistent  ·  Open for new families, closed for edits (OCP)  ·  Reuse of concrete products across the app  ·  You never write new DarkButton() in client code.

STATION 06

Worked Example — Dark / Light UI Theme

Problem: a UI must support a Dark theme and a Light theme, each with its own Button and TextField, without scattering if (dark) … else … everywhere.

  • Products = Button, TextField
  • Factories = DarkThemeFactory, LightThemeFactory
  • The client asks the factory for a Button/TextField and never checks which concrete class comes back.
  • Add a BlueTheme tomorrow → add BlueThemeFactory + BlueButton + BlueTextField. Zero client‑code changes.
«interface» GUIFactory DarkThemeFactory createButton / createTextField LightThemeFactory createButton / createTextField «interface» Button DarkButton LightButton «interface» TextField DarkTextField LightTextField
View the code — GUIFactory family
// Step 1 — Abstract Factory interface
interface GUIFactory {
    Button createButton();
    TextField createTextField();
}

// Step 2 — Concrete Factories, one per theme
class DarkThemeFactory implements GUIFactory {
    Button createButton() { return new DarkButton(); }
    TextField createTextField() { return new DarkTextField(); }
}

class LightThemeFactory implements GUIFactory {
    Button createButton() { return new LightButton(); }
    TextField createTextField() { return new LightTextField(); }
}

// Step 5 — Client only ever talks to the interfaces
GUIFactory factory = new DarkThemeFactory(); // swap this line only
Button   btn = factory.createButton();
TextField tf = factory.createTextField();
STATION 07

The Textbook Example — Computer Store

A computer store sells two families of computers: Advanced and Standard. Each family bundles a Cpu, Monitor, and Keyboard that must stay consistent with each other and pull their info from different data sources.

AdvancedComputerPartsFactory

AdvancedCpu, AdvancedMonitor, AdvancedKeyboard — all read from Source A

StandardComputerPartsFactory

StandardCpu, StandardMonitor, StandardKeyboard — all read from Source B

View the code — Client (ComputerStore)
class ComputerStore {
    Monitor  monitor;
    Cpu      cpu;
    Keyboard keyboard;

    ComputerStore(ComputerPartsFactory factory) {
        // the store never knows if "factory" is Advanced or Standard
        monitor  = factory.createMonitor();
        cpu      = factory.createCpu();
        keyboard = factory.createKeyboard();
    }
}

// main()
ComputerPartsFactory f = (option == 1)
    ? new AdvancedComputerPartsFactory()
    : new StandardComputerPartsFactory();

ComputerStore store = new ComputerStore(f);

Build‑order checklist (memorize this order!)

  1. Design the product interfacesCpu, Monitor, Keyboard.
  2. Identify the families/groups — e.g. Standard vs. Advanced.
  3. Write concrete products for each family, implementing step 1's interfaces.
  4. Create the factory interface — one creation method per product from step 1.
  5. Write a concrete factory per family, implementing step 4's interface.
  6. Wire each concrete factory to its own products from step 3.
  7. Build the Client, depending only on the interfaces from steps 1 and 4.
Memory hook "PIP-FICO" the build order

Product interfaces → Identify families → Products (concrete) → Factory interface → Individual concrete factories → Connect factory↔product → One client at the end.

STATION 08

Bonus Example — Riyadh HQ Car Factories

Riyadh HQ (the client) only ever talks to a CarFactory interface with makeMercedes() and makeBMW(). It never touches JeddahCarFactory or DammamCarFactory directly.

  • Why HQ doesn't care who builds the car: HQ codes against the abstract CarFactory, Mercedes, and BMW types — the concrete branch is an implementation detail hidden behind those interfaces.
  • Why it's easy to switch branches at runtime: swapping JeddahCarFactory for DammamCarFactory is a one‑line change (which object gets assigned) — no recompiling or editing of HQ's logic.
  • Why this is Abstract Factory: two concrete factories (branches) each produce their own consistent family of two related products (Mercedes + BMW), behind one shared interface.
STATION 09

"But We Still Write Concrete Classes…?"

Yes — DarkButton, LightButton, DarkThemeFactory, etc. all get written somewhere. The pattern's promise isn't "no concrete classes exist" — it's that the client never mentions their names. The client only ever declares variables of the abstract types (Button, GUIFactory…), so adding a brand‑new family later touches zero client code.

Memory hook "The client says WHAT, never WHICH."

WHAT it needs (a Button) — never WHICH concrete class makes it.

STATION 10

Pros & Cons

✅ Pros

  • Isolates concrete product classes → easier to reuse them
  • Keeps each product family internally consistent
  • New families = pure extension, existing code untouched (Open/Closed Principle)
  • Increases overall modifiability

⚠️ Con

  • Requires a large number of classes — one interface + N concrete implementations, times every product
Memory hook "More classes now, fewer headaches later."
STATION 11

Quick Self‑Test — Flip the Cards

Tap a card to reveal the answer. Cover the section above first!

What is the Abstract Factory's intent?tap
Provide an interface for creating families of related objects without exposing their concrete classes.
Class‑level vs Object‑level scope?tap
Class‑level = fixed at design/compile time. Object‑level = decided at runtime (Abstract Factory is object‑level).
Name the 5 Creational patternstap
Singleton, Prototype, Factory Method, Abstract Factory, Builder — "Some People Find Amazing Builders".
What happens when you add a new family?tap
You add a new Concrete Factory + new Concrete Products. Existing client code is not modified (OCP).
Biggest downside?tap
A large number of classes — one interface plus one implementation per family, for every product.
Creator classes vs Product classes?tap
Creator = the (Abstract/Concrete) Factories. Product = the (Abstract/Concrete) items the factories build.
STATION 12

Chapter Recap & What's Next

This chapter covered pattern fundamentals (purpose vs. scope, documentation categories) and the first creational pattern: Abstract Factory — one interface, many families, client stays blind to the concrete classes.

Up next in Creational Patterns: Factory MethodBuilderSingleton