← Back to courses

Design Patterns

23 Gang of Four patterns · Creational, structural, behavioral · Node.js examples

01Creational Patterns- How objects are created without hard-coding construction everywhere
02Structural Patterns- How objects and modules are composed into larger shapes
03Behavioral Patterns- How objects communicate, delegate work, and manage behavior
Chain of Responsibilitybehavioral
Pass a request through handlers until one handles it.
Request -> auth -> validation -> controller -> response
e.g.: Express middleware, Koa middleware, error pipelines
Commandbehavioral
Wrap an action as an object so it can be queued, logged, or undone.
Client creates command -> queue/log/execute -> optional undo
e.g.: BullMQ jobs, task queues, undo stacks
Interpreterbehavioral
Represent grammar rules and evaluate expressions.
Input expression -> parse rules -> evaluate AST
e.g.: Template engines, rule engines, query parsers
Iteratorbehavioral
Traverse a collection without exposing how it is stored.
Collection -> iterator.next() -> item by item
e.g.: for...of, async iterators, streams
Mediatorbehavioral
Centralize communication so objects do not talk directly to each other.
Component A -> Mediator -> Component B/C
e.g.: Event bus, message broker wrappers, NestJS CQRS bus
Mementobehavioral
Capture and restore object state without exposing internals.
state now -> save snapshot -> changes -> restore snapshot
e.g.: Undo history, drafts, editor snapshots
Observerbehavioral
Notify subscribed listeners when state changes.
Subject emits event -> listener A -> listener B -> listener C
e.g.: EventEmitter, RxJS, DOM events, Redis pub/sub
Statebehavioral
Change behavior when an object changes internal state.
Order: pending -> paid -> shipped -> delivered
e.g.: Workflow engines, XState, order lifecycles
Strategybehavioral
Swap algorithms at runtime without changing the caller.
Context -> strategy.pay() -> Stripe / PayPal / Razorpay
e.g.: Passport strategies, payment strategies, sorting strategies
Template Methodbehavioral
Define an algorithm skeleton and let subclasses fill in steps.
generate() -> fetch -> format -> render steps vary
e.g.: Report generators, test runners, ETL jobs
Visitorbehavioral
Add operations to object structures without changing those objects.
Visitor -> visitUser() -> visitOrder() -> visitInvoice()
e.g.: AST transforms, Babel plugins, tree walkers
Design patterns are code-structure patterns. They help objects and modules stay flexible, but they should be used when they simplify real change, not just to make code look formal.