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.
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.
You reuse the plan (structure/roles), you still write the actual classes yourself.
| Type | Deals with… |
|---|---|
| Creational | How objects get created |
| Structural | How objects/classes are composed into bigger structures |
| Behavioral | How objects interact and share responsibility |
| 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) |
And: Class = Compile‑time · Object = happens while Operating (runtime).
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.
Creational patterns abstract and control how objects are created, hiding the concrete classes behind a common creational interface.
Singleton, Prototype, Factory Method, Abstract Factory, Builder.
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:
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.
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.
Button, TextFieldDarkThemeFactory, LightThemeFactoryButton/TextField and never checks
which concrete class comes back.BlueThemeFactory + BlueButton +
BlueTextField. Zero client‑code changes.// 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();
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.
→ AdvancedCpu,
AdvancedMonitor, AdvancedKeyboard — all read from Source A
→ StandardCpu,
StandardMonitor, StandardKeyboard — all read from Source B
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);
Cpu, Monitor,
Keyboard.Product interfaces → Identify families → Products (concrete) → Factory interface → Individual concrete factories → Connect factory↔product → One client at the end.
Riyadh HQ (the client) only ever talks to a CarFactory interface with
makeMercedes() and makeBMW(). It never touches
JeddahCarFactory or DammamCarFactory directly.
CarFactory, Mercedes, and BMW types — the concrete branch
is an implementation detail hidden behind those interfaces.JeddahCarFactory for
DammamCarFactory is a one‑line change (which object gets assigned) — no recompiling
or editing of HQ's logic.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.
WHAT it needs (a Button) — never WHICH concrete class makes it.
Tap a card to reveal the answer. Cover the section above first!
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