DevNotes
Support
Essential Design Patterns · Chapter 01

Introduction to Design Patterns

What design patterns are, the creational / structural / behavioral categories, distributed-systems patterns, and the ten patterns covered in this topic.

◷ 4 min read§ 8 sections? 4 interview questions{ } 0 code examples

#Introduction

A design pattern is a proven, reusable solution to a problem that keeps appearing in software design. It isn't a library or ready-made code — it's a named template for structuring classes and objects, which you adapt to your situation.

The classic catalogue is the Gang of Four (GoF) book, Design Patterns: Elements of Reusable Object-Oriented Software (1994), which described 23 patterns. Modern backend development adds patterns for distributed systems, such as Circuit Breaker and Saga.

#Why Learn Design Patterns?

  • Shared vocabulary — "use a decorator here" says in three words what would otherwise take a paragraph.
  • Proven solutions — they encode lessons about flexibility, coupling and change.
  • Reading frameworks — Spring, Hibernate and the JDK are full of them (proxies, factories, template methods, observers).
  • Interviews — implementing Singleton, Builder, Strategy or Factory is a standard coding question.

#Pattern Categories

  • Design patterns
    • Creationalhow objects are created
      • Factory Method
      • Singleton
      • Builder
    • Structuralhow objects are composed
      • Proxy
      • Adapter
      • Decorator
    • Behavioralhow objects interact
      • Strategy
      • Observer
    • Distributed systemshow services stay resilient and consistent
      • Circuit Breaker
      • Saga
CategoryConcernPatterns in this topic
CreationalCreating objects flexibly, hiding construction detailsFactory Method, Singleton, Builder
StructuralCombining classes and objects into larger structuresProxy, Adapter, Decorator
BehavioralAssigning responsibilities and communication between objectsStrategy, Observer
Microservices / distributedResilience and consistency across servicesCircuit Breaker, Saga

Circuit Breaker and Saga are not GoF patterns — they come from distributed-systems and microservices architecture — but they're asked just as often in backend interviews.

#The Ten Patterns at a Glance

PatternOne wordThe question it answers
Factory MethodCreateWhich object should I create?
SingletonOneHow do I guarantee one shared instance?
BuilderBuildHow do I construct an object with many parameters?
StrategyChooseWhich algorithm or behaviour should I use?
ObserverNotifyWho should be told when something changes?
ProxyControlShould the client be allowed to reach the real object?
AdapterConvert / connectHow can these incompatible interfaces communicate?
DecoratorEnhanceHow can I add optional features without a subclass explosion?
Circuit BreakerProtectShould I keep calling this unhealthy dependency?
SagaCompensateHow do I keep business data consistent across services when a step fails?

#Design Principles Behind the Patterns

Most patterns are applications of a few principles (see the SOLID chapter in Core Java):

PrincipleSeen in
Program to an interface, not an implementationFactory Method, Strategy, Adapter, Proxy, Decorator
Favour composition over inheritanceStrategy, Decorator, Proxy, Adapter
Open/Closed — open for extension, closed for modificationStrategy, Decorator, Observer, Factory Method
Single ResponsibilityBuilder (construction), Proxy (access control), Observer (notification)
Loose couplingObserver, Saga choreography

#Interview Questions

Q1What is a design pattern?

A reusable, named solution to a commonly occurring design problem — a template for organizing classes and objects, not finished code.

Q2What are the categories of GoF design patterns?

Creational (object creation — Factory Method, Singleton, Builder), structural (composition — Proxy, Adapter, Decorator) and behavioral (interaction — Strategy, Observer).

Q3Which design patterns does Spring use?

Singleton (default bean scope), Factory (BeanFactory, FactoryBean), Proxy (AOP, @Transactional), Template Method (JdbcTemplate, RestTemplate), Observer (ApplicationEvent and listeners), Strategy (pluggable implementations injected by interface), Adapter (HandlerAdapter in Spring MVC) and Decorator (wrapping beans, e.g. TransactionAwareDataSourceProxy).

Q4Are Circuit Breaker and Saga GoF patterns?

No — they're distributed-systems / microservices patterns. Circuit Breaker protects callers from failing dependencies; Saga keeps data consistent across services with compensating transactions.

#Common Mistakes

  • Using patterns for their own sake — adding factories and strategies where one class would do.
  • Memorizing UML without the problem — interviewers want to know when and why.
  • Confusing similar patterns — Proxy vs Decorator vs Adapter all wrap objects but for different reasons.

#Key Takeaways

  • A design pattern is a reusable template for a recurring design problem.
  • GoF patterns are creational, structural or behavioral.
  • Circuit Breaker and Saga are microservices patterns for resilience and consistency.
  • Patterns are built on principles: interfaces, composition, Open/Closed.
  • Learn each pattern's problem first; the code follows.