Design Pattern Interview Questions & Answers
Q: What is the Singleton pattern? How is it used in Spring?
A: Singleton ensures a class has only one instance and provides a global point of access. In Spring, beans
are Singleton by default, meaning one instance per Spring container.
Q: Explain the Factory pattern with an example.
A: Factory pattern defines an interface for creating an object, but lets subclasses decide which class to
instantiate.
Example: In a microservice, a NotificationFactory can return EmailNotification, SMSNotification, etc., based
on input.
Q: What is the Builder pattern? When should you use it?
A: Builder pattern helps construct complex objects step by step. It's useful when an object requires multiple
parameters (some optional).
Example: Creating a User object with optional fields like address, phone, etc.
Q: What is the Adapter pattern? Give an example in microservices.
A: Adapter allows incompatible interfaces to work together.
Example: Adapting a legacy SOAP client to work with REST interfaces in your Spring Boot microservice.
Q: Explain the Decorator pattern with an example.
A: Decorator adds behavior to objects at runtime without altering the structure.
Example: Adding logging, rate limiting, or caching to service methods.
Q: What is the Proxy pattern? How does Spring AOP use it?
Design Pattern Interview Questions & Answers
A: Proxy provides a surrogate or placeholder for another object. Spring AOP uses proxies to add
cross-cutting concerns like logging and security.
Q: Explain the Strategy pattern. Where can it be used in Spring Boot?
A: Strategy defines a family of algorithms and lets the algorithm vary independently from clients.
Example: Implementing different payment strategies (CreditCard, PayPal, UPI) using a common
PaymentStrategy interface.
Q: What is the Observer pattern? How is it applied in microservices?
A: Observer lets one object notify other subscribed objects about state changes.
Example: Event-driven architecture using Spring Events or Kafka where multiple services react to events.
Q: What is the Command pattern?
A: Encapsulates a request as an object, allowing for parameterization and queuing.
Example: Queueing requests for a batch job processor in a microservice.
Q: What is the Template Method pattern?
A: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses.
Example: A base class for data import with steps like validate, transform, and save.
Q: Which design patterns are commonly used in Spring Framework?
A: Singleton (Default bean scope)
Factory (Bean creation via BeanFactory)
Proxy (AOP support)
Design Pattern Interview Questions & Answers
Template Method (JdbcTemplate, RestTemplate)
Observer (Spring Events)
Q: What is Dependency Injection and which pattern does it follow?
A: Dependency Injection follows the Inversion of Control (IoC) principle and is implemented using the
Strategy and Factory patterns.
Q: What design patterns are useful in Microservices architecture?
A: API Gateway Pattern
Circuit Breaker (via Resilience4j or Hystrix)
Service Discovery (using Eureka or Consul)
Strangler Fig (gradual migration of legacy to new service)
Saga Pattern (distributed transaction handling)
Q: How does the Circuit Breaker pattern work?
A: It stops the call to a failed service temporarily, preventing the cascading failure. After a timeout, it allows
limited requests to test recovery.