SD&A · Sheet Set 06.02
Chapter 06 · Lecture 02 · Creational Design Patterns

One method. Many products.

Factory Method lets a class defer who gets created to its subclasses — so the client only ever talks to an interface, never a concrete type.

Class Creational One Product at a Time "Defer instantiation to subclasses" Computer Store Case Study
FIG. 01 — QUICK FACTS
Purpose One method creates an object; subclasses decide the concrete class.
Key idea "Defer instantiation to subclasses."
Creates One product at a time.
Type Class creational pattern.
01 · Definition

Factory Method, in one breath

A class creational pattern used to encapsulate and defer object instantiation to derived classes. It defines an interface for creating an object, but lets subclasses decide which class to instantiate.

MEM
"The parent declares the recipe. The child decides the ingredient."

Creator declares createProduct() — it never says which Product. Each ConcreteCreator fills that in.

02 · Structure

4 roles: Creator · ConcreteCreator · Product · ConcreteProduct

Unlike Abstract Factory (many factory methods, many product families), Factory Method needs only one creational interface method for products that share one interface.

Creator
+createProduct()+someOperation()
ConcreteCreator
+createProduct()
«interface» Product
+productOperation1()+createOperation2()
ConcreteProduct
+productOperation1()+productOperation2()
Reading the diagram fast: hollow triangle (△) = inheritance/implements. Anything written in italics on a UML diagram, or marked virtual in C++ code, is abstract — that's your signal it's the Factory Method itself.
MEM
C‑C‑P‑P — Creator, ConcreteCreator, Product, ConcreteProduct.

Two "creator" boxes on the left build; two "product" boxes on the right get built. Same shape, mirrored.

03 · Worked Example

The Computer Store

Scenario: An electronics store sells Standard and Advanced computers. The Standard Store can only build standard computers; the Advanced Store can only build advanced ones. Customers get a common interface — ask for a computer, get back its monitor/CPU/keyboard/cost — and new store categories (Workstation, Budget…) should slot in without touching existing code.

FIG. 02 — COMPUTER STORE CLASS DIAGRAM
ComputerStore
+createComputer(type): Computer*+displayComputer(type): void
StandardComputerStore
+createComputer(type): Computer*
AdvancedComputerStore
+createComputer(type): Computer*
«interface» Computer
+displayMonitorInfo()+displayCpuInfo()+displayKeyboardInfo()+displayCost()
StandardComputer
displayMonitorInfo() ...
AdvancedComputer
displayMonitorInfo() ...

ComputerStore (abstract)

Declares the factory method createComputer() and a helper displayComputer() that calls it, then prints whatever it gets back.

Derived stores

StandardComputerStore / AdvancedComputerStore each override createComputer() to return their own concrete Computer.

MEM
Summary in one line: the Factory Method pattern makes new Computer types possible without changing the client code — the creation logic lives only in ComputerStore and its children.
04 · Design Recipe

5 steps to build any Factory Method

MEM
"Products get Concrete, Factories get Concrete, then Associate them." (PCFCA)

Design the product interface

e.g. Computer, Parser, Channel.

Design the concrete products

Each realizes the interface. e.g. StandardComputer, AdvancedComputer.

Design the factory interface (Creator)

One abstract method that delegates product creation to derived classes. e.g. ComputerStore.

Design one concrete factory per product

e.g. StandardComputerStore, AdvancedComputerStore.

Associate each factory with its product

Wire each ConcreteCreator's factory method to return the matching ConcreteProduct.

Alternate build order (from the "general order for writing classes" note): 1) Product interface → 2) Concrete Products → 3) Creator (abstract class) → 4) Concrete Creators → 5) Client / main. Same five ingredients, ordered the way you'd actually type the files.
05 · Reading the Code

C++ walk‑through: ComputerStore

The three pieces below are the same class split across declaration and two implementations — read them top to bottom and the pattern clicks.

// The Creator — abstract because instantiate() is virtual class ComputerStore { public: // The factory method itself virtual Computer* createComputer(std::string type); // Uses the factory method — doesn't know which Computer it gets void displayComputer(std::string type); }; // displayComputer() delegates creation, then just prints the result void ComputerStore::displayComputer(string type) { Computer* computer = createComputer(type); // delegated! computer->displayMonitorInfo(); computer->displayCpuInfo(); computer->displayKeyboardInfo(); computer->displayCost(); } // ConcreteCreator — implements the factory method Computer* StandardComputerStore::createComputer(string type) { if (type.compare("standard") == 0) return new StandardComputer; return new NullComputer; }

C++Spot the abstract method

Look for the virtual keyword — that marks the factory method.

UMLSpot it on a diagram

Look for methods written in italics — same meaning, different notation.

06 · Payoff

Benefits — S·E·R·M·T

SSeparates

Client code from product‑specific classes — the same code works with existing or new product classes.

EEfficient

Different developers can build different concrete creators/products at the same time.

RReuse

Easier to reuse specific parts of the code in other systems.

MMaintain

Easier to maintain specific parts without touching the rest.

TTestability

Improves testability — swap in a fake ConcreteCreator for tests.

MEM
"Sarah Eats Rice, Mostly Tuesdays." (SERMT)
07 · Don't Confuse Them

Factory Method vs. Abstract Factory

Aspect Factory Method Abstract Factory
Purpose One factory method creates one product at a time. Interface for creating families of related products.
Factory One factory method (often parameterized); subclasses decide the product. Factory interface with multiple factory methods (one per product type).
Products Usually one hierarchy — e.g. Parser → PdfParser, WordParser. Multiple related hierarchies — e.g. Button+Checkbox families for Windows/Mac.
Flexibility Lightweight — one kind of product needed. More complex — guarantees consistency across a family.
Client calls A single method, e.g. createParser(). Multiple methods, e.g. createButton() + createCheckbox(), always matched.
MEM
"One product, one method → Factory Method. Many products, many methods, one family → Abstract Factory."
08 · Practice Scenarios

The two extra questions from your slides

Both are the exact same skeleton as the Computer Store — swap the nouns and the answer barely changes. That repetition is the exam prep.

Scenario A

Document Reader framework (PDF / Word / Excel)

Abstract DocumentReader declares one overridable method that returns the Parser to use. Each concrete reader (PdfReader, WordReader, ExcelReader) overrides it to return its own parser. Adding PowerPoint support = add one new reader subclass, zero changes to existing client code.

Creator = DocumentReader Factory method = createParser() Product = Parser interface ConcreteProducts = PdfParser / WordParser / ExcelParser

Answer: Factory Method — one product family (Parser), one factory method, subclasses decide.

Scenario B

Notification Sender framework (Email / SMS / Push)

Abstract NotificationSender implements the fixed workflow — validate → createChannel() → send. Each concrete sender (EmailSender, SmsSender, PushSender) overrides createChannel() to return its own Channel. Adding WhatsApp = one new sender + one new channel class.

Creator = NotificationSender Factory method = createChannel() Product = Channel interface ConcreteProducts = EmailChannel / SmsChannel / PushChannel

Answer: Factory Method again — same shape as Scenario A, different domain.

Pattern‑recognition trigger phrase: whenever a question says "a base class implements the overall workflow, and declares one overridable method whose job is to return the [product] to use" — that's always Factory Method. Count the overridable methods: one → Factory Method, several (one per product type) → Abstract Factory.
09 · Memory Toolkit

All the mnemonics, in one place

1Core idea

"The parent declares the recipe. The child decides the ingredient."

2Structure

CCPP — Creator, ConcreteCreator, Product, ConcreteProduct.

3Spotting abstract

virtual in code = italics in UML.

4Design recipe

PCFCA — Product, Concrete products, Factory, Concrete factories, Associate.

5Benefits

SERMT — Separates, Efficient, Reuse, Maintain, Testability.

6vs. Abstract Factory

One product/one method → Factory Method. Many products/many methods/one family → Abstract Factory.