Skip to main content

Module 4: Structural & Creational Patterns

Primary texts: Head First Design Patterns (approachable narratives), Design Patterns by GoF (canonical vocabulary) Selective support: Refactoring, Clean Code, Good Code, Bad Code, and OOAD chunks where they sharpen construction, bridging, composition, or injection judgment

This guide is the primary teacher. You do not need to read the pattern books end to end. You do need to become operationally strong at choosing which pattern (if any) relieves real pressure, implementing it cleanly in one of your working languages, and recognizing the misuses that turn patterns into ceremony.


Scope of This Module

This module is not a pattern catalog to memorize. It is where you learn to tell the difference between a pattern that solves a real problem and a pattern that has been bolted on because it felt professional.

What it covers in depth:

  • creational control: Factory Method, Abstract Factory, Builder, and the narrower role of Prototype and Singleton
  • structural interface bridging: Adapter to reconcile mismatched APIs, Facade to hide subsystem complexity, Bridge to decouple abstraction from implementation
  • structural composition: Composite to treat a tree as a leaf, Decorator to stack behavior without a subclass explosion, Proxy to intercept access
  • pragmatic selection: pattern categories, cargo-culting warning signs, and when a closure or a function is the real answer
  • dependency injection as a design technique and composition roots as the single place wiring happens

What it deliberately does not try to finish here:

  • the entire GoF catalog (behavioral patterns are in Module 3; a few minor patterns are referenced but not drilled)
  • DI container configuration for specific frameworks (touched only as composition-root examples)
  • architectural patterns beyond single-process design (those belong to later systems modules)

If you can draw a class diagram but cannot explain what problem the pattern is solving in the running code, you are not done.


Before You Start

Answer these closed-book before starting the main path:

  1. Why is new ConcreteType() inside business logic sometimes a design problem, and sometimes the correct choice?
  2. What is the difference between an Adapter and a Facade?
  3. Why can a Decorator add behavior without modifying the decorated class?
  4. When does adding a factory hurt the reader more than it helps?
  5. What does "inject your dependencies" mean in plain code terms, not framework terms?

Diagnostic Interpretation

4-5 solid answers

  • You are ready for the full path and can move quickly through the first cluster.

2-3 solid answers

  • Continue, but expect extra time on Adapter vs Facade and on DI.

0-1 solid answers

  • Revisit Module 1 (cohesion and coupling) and Module 3 (Strategy and Command) first. These patterns only make sense once you believe "composition over inheritance" at a gut level.

What This Module Is For

These patterns are not the point. The forces behind them are the point. Later engineering work repeatedly asks questions like:

  • where do I put the if that chooses between concrete implementations?
  • how do I integrate a third-party library without letting its shape leak everywhere?
  • how do I add a feature without subclassing or rewriting the caller?
  • how do I make this class testable without mocking half the world?
  • where in the application does the wiring happen, and how do I keep it out of the domain?

This module builds the design reasoning needed for:

  • plugin architectures, strategy registration, and configuration-driven behavior
  • legacy integration, third-party SDK wrapping, and anti-corruption layers
  • middleware pipelines, request decoration, and access control
  • dependency-injected, test-first services and composition roots
  • informed code review where you can name the force the pattern is solving

You are learning to reach for a pattern only when plainer options run out.


Concept Map


How To Use This Module

Work in order. The later clusters only make sense if the earlier construction and bridging habits are stable.

Cluster 1: Creational -- Construction Flexibility

OrderConceptTypeFocus
1Factory MethodPRIMARYLetting subclasses choose the concrete product
2Abstract FactoryPRIMARYFamilies of related objects that must match
3BuilderPRIMARYFluent, step-wise construction of complex values
4Prototype and SingletonSUPPORTINGWhen copying or single-instance is right, and when it is a smell

Cluster mastery check: Can you explain in one sentence which force each creational pattern relieves, and when the right answer is just new?

Cluster 2: Structural -- Interface Bridging

OrderConceptTypeFocus
5AdapterPRIMARYReconciling incompatible interfaces
6FacadePRIMARYSimplifying a subsystem's API for the common case
7BridgeSUPPORTINGSeparating an abstraction from its implementation hierarchy

Cluster mastery check: Can you tell an Adapter from a Facade from a Bridge without drawing the canonical diagram?

Cluster 3: Structural -- Composition and Layering

OrderConceptTypeFocus
8CompositePRIMARYTreating an individual and a group uniformly
9DecoratorPRIMARYLayering behavior without a subclass explosion
10ProxyPRIMARYControlling access, laziness, and indirection

Cluster mastery check: Can you distinguish Decorator from Proxy by the intent of the wrapper, not just its structure?

Cluster 4: Pragmatic Pattern Selection

OrderConceptTypeFocus
11Pattern Categories and When to Apply ThemPRIMARYCreational / structural / behavioral as a selection aid
12Pattern Misuse and Cargo-Culting Warning SignsPRIMARYThe smells that mean the pattern is hurting you
13Simpler Alternatives: Closures, Higher-Order Functions, CompositionSUPPORTINGWhen a function or a pair of callbacks is the real pattern

Cluster mastery check: Can you argue both sides of "should we introduce a factory here?" and pick one with a reason?

Cluster 5: Dependency Injection and Composition Roots

OrderConceptTypeFocus
14Dependency Injection as a Design TechniquePRIMARYPassing collaborators in, not constructing them inside
15Composition Roots and Configuration BoundariesSUPPORTINGOne place where wiring happens, nowhere else

Cluster mastery check: Can you locate the composition root of an existing codebase and justify why everything outside it is new-free?

Then work these practice pages:

OrderPractice pathFocus
1Creational Patterns LabFactory Method, Abstract Factory, Builder in real code
2Interface Bridging WorkshopAdapter and Facade against a real third-party shape
3Composition and DI ClinicComposite, Decorator, Proxy, and the DI wiring that makes them testable
4Code KatasShort deliberate-practice drills on all major patterns

Use Module Quiz after the concept and practice path. Use Reference and Selective Reading and Learning Resources only for targeted reinforcement.


Learning Objectives

By the end of this module you should be able to:

  1. Choose between Factory Method, Abstract Factory, Builder, and a plain constructor, with a written reason.
  2. Implement Adapter and Facade to isolate third-party or legacy code from your domain.
  3. Use Composite to hide the difference between an individual and a group.
  4. Use Decorator to add cross-cutting behavior without modifying the decorated class.
  5. Use Proxy to control access, defer work, or remote a call.
  6. Recognize cargo-culted patterns by smell: inheritance-for-reuse, ceremony without variation, and abstraction-without-need.
  7. Refactor pattern-heavy code into closures or higher-order functions where the language allows.
  8. Inject dependencies through constructors and tell why service locators are usually worse.
  9. Identify a composition root and keep wiring out of the domain.
  10. Defend a pattern choice to a skeptical reviewer in fewer than five sentences.

Outputs

  • a pattern notebook with at least 15 solved implementations, one per concept, in a language of your choice
  • one "wrapped third party" exercise: Adapter + Facade around a real SDK or HTTP client
  • one decorator pipeline for a real cross-cutting concern (logging, retry, caching, or auth)
  • one small app (CLI, HTTP service, or game loop) that has a named, isolated composition root
  • one refactoring before-and-after that removes a cargo-culted pattern and replaces it with a closure, a first-class function, or plain composition
  • one mistake log naming at least 10 pattern misuses such as singleton-for-globals, factory-with-one-product, adapter-leaking-adaptee-types, decorator-breaking-equality, or new-inside-a-domain-service
  • one short memo explaining how Module 4 patterns land in Module 5 (review) and in later systems work (frameworks, plugin loading, test doubles)

Completion Standard

You have completed Module 4 when all of these are true:

  • you can justify each pattern choice by naming the specific change pressure it is absorbing
  • you can tell Adapter from Facade, Decorator from Proxy, and Factory Method from Abstract Factory without hedging
  • you can implement each primary pattern from memory in ten minutes or less
  • you can delete a pattern that is not earning its keep and leave the code better
  • you can wire a small application through a composition root and leave the domain pattern-free
  • you can argue the position that "a function would be enough here" when it is true, and the opposite when it is not

If the answer looks like "I memorized the diagram," the module is not complete.


Reading Policy

  • Concept pages are the main path.
  • Local book chunks are selective reinforcement, not a second syllabus.
  • Read only if stuck means try the concept page, write the pattern in your own code, and review the mini drill first.
  • Optional deep dive means additional nuance, alternate languages, or history, not required progression.
  • Pattern names are vocabulary; earned reasons are the real deliverable.

Suggested Weekly Flow

DayWork
1Concepts 1-4 and one Factory Method + Builder implementation
2Concepts 5-7 and one Adapter wrapping a real third-party shape
3Concepts 8-10 and a Decorator pipeline with at least three layers
4Concepts 11-13 and one "delete a pattern" refactor
5Concepts 14-15 and one composition-root sketch for a real app
6Practice pages 1-2 and targeted chunk reinforcement
7Practice pages 3-4, quiz, and mistake-log cleanup

Reference

If you need exact links into the local chunked books, use Reference and Selective Reading.


Rich Learning Pages

Worked Examples | Guided Labs | Case Studies | Mistake Clinic | Reading Guide | Capstone Thread