Roadmap › 1. .NET › Step 12
DP

Design Patterns (GoF + Enterprise) in C#

Creational, structural and behavioural Gang-of-Four patterns with C# code, plus Repository, Unit of Work, CQRS, Mediator, Options and microservice patterns.
Quick revision for interview day. Print it or save it as a PDF.

23 GoF patterns — one line each

PatternOne line
SingletonOne instance (Lazy<T> / AddSingleton)
Factory MethodSubclass/method decides concrete type
Abstract FactoryFamily of related objects
BuilderStep-by-step construction, fluent
PrototypeClone existing object
AdapterConvert interface (wrap SDK)
BridgeSplit abstraction from implementation
CompositeTree; leaf and group treated alike
DecoratorWrap to add behaviour, same interface
FacadeSimple API over subsystem
FlyweightShare immutable state
ProxyStand-in controlling access (lazy, remote, auth)
Chain of ResponsibilityPass along handlers (middleware)
CommandRequest as object (queue, undo)
InterpreterEvaluate a grammar
IteratorSequential access (IEnumerable)
MediatorCentral communication (MediatR)
MementoSnapshot/restore state
ObserverSubscribe/notify (events)
StateBehaviour depends on state object
StrategyInterchangeable algorithms
Template MethodBase skeleton, subclass steps
VisitorNew operations without changing classes

Patterns hiding in .NET

.NET featurePattern
Middleware, DelegatingHandlerChain of Responsibility
IHttpClientFactory, ILoggerFactoryFactory
WebApplication.CreateBuilderBuilder
Stream wrappersDecorator
event / IObservableObserver
IEnumerable + yieldIterator
DbContext / DbSetUnit of Work / Repository
EF lazy-loading proxiesProxy
IComparer<T>Strategy
ExpressionVisitorVisitor

Enterprise/cloud

  • Repository + UoW — data access abstraction + one transaction.
  • CQRS — separate reads and writes.
  • Outbox — event + data in the same transaction.
  • Saga — distributed transaction with compensation.
  • Retry / Circuit Breaker / Timeout / Bulkhead — resilience (AddStandardResilienceHandler).
  • Options — typed config: IOptions / IOptionsSnapshot / IOptionsMonitor.
  • Strangler Fig — replace a legacy system piece by piece.
  • API Gateway / BFF — single entry point per client type.

Answer formula

Problem → pattern name → how it works (2 lines) → code shape (interface + classes) → where you used it → trade-off.

Which pattern for which problem?

Problem in the interview questionPattern
Big switch on type / many interchangeable algorithmsStrategy
Add caching/logging/retry without touching a classDecorator
Wrap a third-party SDK behind your own interfaceAdapter
One simple call over many subsystemsFacade
Object creation varies by input/configFactory Method / Abstract Factory
Complex object with many optional partsBuilder
Exactly one shared instanceSingleton (prefer AddSingleton)
Notify many listeners of changesObserver / events
Request passes through a series of handlersChain of Responsibility
Behaviour depends on the object's stateState
Fixed algorithm with customisable stepsTemplate Method
Decouple senders and handlers (CQRS)Mediator / Command
Reliable event publishing with DB writesOutbox
Flaky downstream serviceRetry + Circuit Breaker