Design Patterns
design patterns design patterns are recurring solutions to recurring problems — a vocabulary that lets senior engineers communicate. knowing them is interview
Introduction
Design patterns are recurring solutions to recurring problems — a vocabulary that lets senior engineers communicate. Knowing them is interview gold and refactor fuel.
Understanding the topic
The patterns you actually use weekly:
- Strategy — swap an algorithm at runtime (sorting, payment, pricing).
- Factory — centralise object creation behind a method.
- Builder — assemble complex objects step-by-step (think Lombok's
@Builder). - Singleton — exactly one instance (use sparingly; DI usually replaces it).
- Observer — publish/subscribe; the JDK ships
java.util.concurrent.Flow. - Decorator — wrap to add behaviour without subclassing (Java IO is built on this).
Informative example
Strategy pattern, modern Java:
// One interface, many implementations chosen at runtimeinterface PricingStrategy {int priceCents(int quantity);}record Standard() implements PricingStrategy {public int priceCents(int q) { return q * 1000; }}record VolumeDiscount() implements PricingStrategy {public int priceCents(int q) { return q >= 10 ? q * 800 : q * 1000; }}class Cart {private final PricingStrategy strategy;Cart(PricingStrategy s) { this.strategy = s; }int total(int q) { return strategy.priceCents(q); }}
Best practices
- Don't apply a pattern just because you can — only when it removes pain.
- Records + interfaces often replace Builder/Strategy boilerplate.
Purpose of this lesson
Master Design Patterns so you can apply it confidently in production Java code, technical interviews, and code reviews.
Step-by-step explanation
- Understand the core idea behind Design Patterns.
- Walk through the runnable example and tweak it in the playground.
- Apply the pattern in a small Spring Boot or CLI exercise of your own.
- Re-read the common mistakes and interview Q&A to lock the concept in.
Interactive workflow diagram
Identify use case
Recognize when design patterns is the right tool for the problem.
Debugging tips
- Read the full stack trace — Java's exception messages name the offending class and line.
- Reproduce in the smallest possible
main()method before fixing in the real app. - Use IntelliJ's debugger breakpoints and 'Evaluate Expression' rather than scattering
System.out.
Optimization strategies
- Profile before optimizing — JFR (Java Flight Recorder) and async-profiler reveal real hotspots.
- Prefer immutable data and stream pipelines over hand-rolled loops when readability matters.
- Reach for the right JDK collection (ArrayList vs LinkedList vs ArrayDeque) before writing custom data structures.
Enterprise example
Teams at Netflix, Uber and Goldman Sachs apply Design Patterns daily — usually wrapped behind Spring Boot services with observability hooks (Micrometer + OpenTelemetry).
Interview questions & answers
Q1Singleton vs static class?
Q2When do you reach for the Strategy pattern?
Summary
In this lesson you learned Design Patterns — the concept, syntax, a runnable example, and the production pitfalls to avoid. Apply it in the playground before moving on.