# Design Patterns for Backend Code

> Use classic Gang of Four and enterprise patterns to structure code, and recognize when they are overkill. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/backend/design-patterns-gof
Last updated: 2026-11-28

Use classic Gang of Four and enterprise patterns to structure code, and recognize when they are overkill.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Monoliths and Modular Monoliths](https://www.internhack.in/roadmaps/tracks/backend/monolith-modular)

**Design patterns** are named, reusable solutions to recurring design problems. Knowing them gives you vocabulary ("wrap it in an adapter") and ready-made shapes, as long as you apply them to real problems rather than to show off.

### Gang of Four patterns you will actually use

- **Strategy**: swap algorithms behind an interface (different pricing or shipping calculators).
- **Adapter**: wrap a third-party API (a payment provider) behind your own interface so it can be replaced and mocked.
- **Factory**: centralize object creation when the concrete type depends on input or config.
- **Decorator**: add behavior (caching, logging, retries) around an object without changing it; HTTP middleware is the same idea.
- **Observer**: notify subscribers of events; the basis of in-process event emitters.
- **Singleton**: one shared instance (a DB pool). Use sparingly; it becomes hidden global state that complicates tests.
- **Builder** and **Facade** for complex construction and simplifying a messy subsystem.

### Enterprise application patterns

- **Repository**: a collection-like interface over data access, keeping SQL out of business logic.
- **Unit of Work**: track changes and commit them in one transaction (what most ORMs do).
- **Dependency injection**: pass collaborators in rather than creating them inside, which makes testing easy. Frameworks like NestJS, Spring and ASP.NET build on it.
- **Middleware / chain of responsibility** for request pipelines.

### Principles behind them

**SOLID**, composition over inheritance, and "program to an interface" explain why the patterns work.

### Pitfalls

Pattern-itis: factories of factories and interfaces with one implementation that never changes. Start simple and refactor to a pattern when the pain appears.

You are ready to move on when you can spot three patterns in your framework of choice and refactor one piece of your code to use Adapter or Strategy with a test proving the swap works.

## Key concepts

- **Strategy and Adapter**
- **Factory and Builder**
- **Decorator and middleware**
- **Observer**
- **Repository and Unit of Work**
- **Dependency injection**
- **SOLID principles**
- **Singleton and its pitfalls**

## Mini project

Wrap two email providers behind one Adapter interface, select between them with a Strategy based on config, inject it into your service, and unit test the service with a fake.

## Check yourself

How does HTTP middleware relate to the Decorator and Chain of Responsibility patterns?

## Free resources

- [Refactoring.Guru: Design Patterns](https://refactoring.guru/design-patterns) (Refactoring.Guru)
- [Catalog of Patterns of Enterprise Application Architecture](https://martinfowler.com/eaaCatalog/) (Martin Fowler)
- [Inversion of Control Containers and the Dependency Injection pattern](https://martinfowler.com/articles/injection.html) (Martin Fowler)

Previous: [Service Mesh](https://www.internhack.in/roadmaps/tracks/backend/service-mesh) · Next: [Domain-Driven Design](https://www.internhack.in/roadmaps/tracks/backend/domain-driven-design)

## More in Architecture and Design Patterns

- [Monoliths and Modular Monoliths](https://www.internhack.in/roadmaps/tracks/backend/monolith-modular)
- [Microservices and Service-Oriented Architecture](https://www.internhack.in/roadmaps/tracks/backend/microservices-soa)
- [Serverless and Functions as a Service](https://www.internhack.in/roadmaps/tracks/backend/serverless)
- [Twelve-Factor Apps and Cloud-Native Habits](https://www.internhack.in/roadmaps/tracks/backend/twelve-factor-apps)
- [Service Mesh](https://www.internhack.in/roadmaps/tracks/backend/service-mesh)
- [Design Patterns for Backend Code](https://www.internhack.in/roadmaps/tracks/backend/design-patterns-gof)
- [Domain-Driven Design](https://www.internhack.in/roadmaps/tracks/backend/domain-driven-design)
- [CQRS and Event Sourcing](https://www.internhack.in/roadmaps/tracks/backend/cqrs-event-sourcing)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
