Design Patterns - Node.js Reference Map
// 23 Gang of Four patterns ยท Creational, structural, behavioral ยท Node.js examples
23 total patterns
Creational
Structural
Behavioral
01Creational Patterns- How objects are created without hard-coding construction everywhere
๐Singletoncreational
Ensure only one shared instance exists for a resource.
getInstance()
-> exists? return same
-> missing? create once
e.g.: Mongoose connection, Winston logger, config manager
๐ญFactory Methodcreational
Create objects through a factory instead of calling constructors directly.
create(type)
email -> EmailNotifier
sms -> SMSNotifier
push -> PushNotifier
e.g.: Express middleware factories, Sequelize dialects
๐งฐAbstract Factorycreational
Create families of related objects behind one common interface.
DBFactory
-> MySQL connection + query
-> Mongo connection + query
e.g.: TypeORM, Sequelize dialect system, cloud provider SDK wrappers
๐งฑBuildercreational
Build complex objects step by step with a fluent API.
builder.select()
-> from()
-> where()
-> limit()
-> build()
e.g.: Knex.js, Mongoose query API, request builders
๐งฌPrototypecreational
Clone an existing object and customize the copy.
base object
-> clone()
-> override fields
-> new variant
e.g.: Object.create(), structuredClone(), config cloning
02Structural Patterns- How objects and modules are composed into larger shapes
๐Adapterstructural
Translate one interface into another interface your app expects.
Your code expects send(msg)
-> Adapter
-> SDK uses deliver({ body })
e.g.: AWS SDK wrappers, Twilio adapters, payment adapters
๐Bridgestructural
Separate abstraction from implementation so both can vary independently.
Report
-> Renderer interface
-> PDFRenderer / HTMLRenderer
e.g.: Renderer plugins, storage drivers, notification channels
๐ณCompositestructural
Treat single objects and groups of objects the same way.
Folder
-> File
-> Folder
-> File
call size() on any node
e.g.: File trees, React component trees, menu trees
๐Decoratorstructural
Wrap an object to add behavior without changing the original class.
Base service
-> Auth wrapper
-> Log wrapper
-> Cache wrapper
e.g.: Express middleware, NestJS decorators, service wrappers
๐๏ธFacadestructural
Expose one simple API over a complex subsystem.
placeOrder()
-> reserve inventory
-> charge payment
-> send email
e.g.: Express app.listen(), Mongoose.connect(), SDK clients
๐ชถFlyweightstructural
Share repeated immutable data to reduce memory usage.
many objects
-> shared intrinsic state
-> per-item external state
e.g.: Object pools, string interning, cached metadata
๐ก๏ธProxystructural
Use a stand-in object to control access to the real object.
Client
-> Proxy checks auth/cache/rate
-> Real service
e.g.: JavaScript Proxy, ORM lazy loading, caching proxy
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.