Java Patterns Interview Questions
48 questions with answers · Java Interview Guide
Singleton, Factory, Strategy, Observer, Builder, and other design patterns commonly asked in Java interviews.
What design patterns do you know
Common patterns: Singleton, Factory, Strategy, Observer, Adapter, Decorator, Proxy, Builder, Template Method, and State.
class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
class Prototype implements Cloneable {
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}How Singleton differs from Prototype
Singleton creates one instance (lazy or eager initialization, thread-safe), Prototype creates copies of existing objects; Singleton manages instance count, Prototype manages object cloning.
class Singleton {
private static Singleton instance;
public static synchronized Singleton getInstance() {
if(instance == null) instance = new Singleton();
return instance;
}
}
Prototype p1 = new Prototype();
Prototype p2 = (Prototype) p1.clone();Why is singleon called antipattern
Singleton violates SOLID principles: tight coupling, hard to test (difficult to mock), breaks single responsibility, and creates global mutable state. It complicates dependency injection and multi-threaded scenarios.
class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if(instance == null) instance = new Singleton();
return instance;
}
}What design templates do you know
Common design patterns include Singleton, Factory, Strategy, Observer, Decorator, Adapter, Builder, Template Method, and Proxy for solving recurring design problems.
What design patters do you know
Singleton, Factory, Builder, Observer, Strategy, Adapter, Decorator, Proxy, Template Method, Command, State, Chain of Responsibility patterns for different structural/behavioral needs.
What is the difference between the Builder and Facade design template
Builder provides flexible step-by-step object construction with method chaining; Facade simplifies complex subsystem interactions by providing a unified interface to multiple components.
What is the lack of desing patterns
Design patterns lack flexibility for evolving requirements,over-engineering with unnecessary patterns increases complexity; patterns should solve actual problems, not be applied dogmatically.
What are design patterns
Reusable solutions to common design problems: creational (Builder, Singleton), structural (Adapter, Decorator), behavioral (Strategy, Observer).
Tell me about your design-patters experience
Used Factory and Builder for object creation, Strategy for business logic, Observer for event handling, and Decorator for cross-cutting concerns.
What design patterns do you use
Singleton, Factory, Builder, Strategy, Decorator, Observer, and Repository patterns depending on use-case requirements.
What synchronization channels used to organize the interaction of several services
Message queues (RabbitMQ, Kafka), REST/gRPC APIs, event buses, and pub-sub mechanisms for asynchronous inter-service communication.
As if organized the interaction of several services
Services communicate via REST APIs, message queues (async), gRPC for low-latency, or event-driven architectures with event buses.
What are the pluses of microservices compare to monolith
Independent scaling, polyglot persistence, fault isolation, faster deployments, and team autonomy vs monolith's coupling and scaling limitations.
Tell me something about microservice interaction
Synchronous REST/gRPC for tight coupling with timeouts, asynchronous messaging for decoupling, service mesh for observability, and circuit breakers for resilience.
Which structure acts as quickly as possible to the Comand pattern than it can be replaced
The Command pattern itself executes quickly; if performance is critical, use a queue/thread pool structure to batch commands rather than execute individually.
Why do you need a pattern Command
Command pattern encapsulates requests as objects, enabling queuing, undo/redo, logging, and delayed execution independent of the invoker.
Whether you use the Comand pattern in work
Yes, Command pattern is commonly used for undo/redo mechanisms, macro recording, request queuing, and asynchronous task execution in real applications.
What is the advantage of the Builder pattern over the constructor
Builder pattern separates construction logic from representation, handles complex multi-step object creation more readably, and supports fluent interface compared to multiple constructor overloads.
Which structure acts as quickly as possible to the Comand pattern than it can be replaced
Queue or Stack structures process commands in FIFO/LIFO order efficiently, enabling Command pattern implementation through queued execution.
Why do you need a pattern Command
Command pattern encapsulates requests as objects, enabling queuing, logging, undo/redo functionality, and decoupling invoker from receiver.
Whether you use the Comand pattern in work
Yes, extensively used in business logic services for encapsulating operations like payments, notifications, and audit logging as command objects.
What is the advantage of the Builder pattern over the constructor
Builder pattern allows flexible object construction with validation and readability, avoiding telescoping constructors and allowing partial initialization of complex objects.
What patterns used besides Singleton
Common patterns: Factory, Strategy, Observer, Decorator, Adapter, Template Method, Proxy, Chain of Responsibility, and Dependency Injection patterns.
Where you can use singleton
Singleton pattern is used for stateless services, logging, database connections, caching, and configuration management where only one instance should exist application-wide.
What tasks did the patterns solved
Design patterns solve recurring problems: Singleton manages single instances, Factory simplifies object creation, Observer enables event handling, Strategy allows algorithm switching, Proxy controls access.
What are the main characteristics of the templates
Design pattern characteristics: reusable solutions to common design problems, provide templates for code structure, improve maintainability, promote best practices, and facilitate communication between developers.
Types of design templates
Design pattern types: Creational (object creation), Structural (object composition and relationships), Behavioral (communication between objects and responsibility distribution).
Give examples of the main design templates
Main patterns: Singleton, Factory, Builder (Creational); Adapter, Decorator, Facade, Proxy (Structural); Observer, Strategy, Command, State, Template Method (Behavioral).
Give examples of generating design templates
Creational patterns: Singleton (thread-safe instance), Factory (object creation abstraction), Builder (complex object construction), Prototype (cloning objects), Abstract Factory (family of related objects).
Give examples of structural design templates
Structural patterns: Adapter (interface compatibility), Decorator (dynamic behavior addition), Facade (simplified interface), Proxy (controlled access), Bridge (abstraction decoupling), Composite (tree structures).
Give examples of behavioral design templates
Behavioral patterns: Observer (event notification), Strategy (algorithm switching), State (object state changes), Command (action encapsulation), Iterator (collection traversal), Template Method (algorithm skeleton).
What is "antipatttern", what antipatterns you know
Antipattern is a proven ineffective solution; examples: God Object (too many responsibilities), Spaghetti Code (tangled logic), Circular dependencies, Premature optimization, Magic numbers/strings without constants.
What tasks did the patterns solved
Patterns solved problems like managing object creation complexity, reducing coupling between components, and enabling flexible behavior without modifying core classes.
Where you can use singleton
Singleton for loggers, configuration managers, thread pools, and database connection pools where you need a single instance shared across the application.
What patterns used besides Singleton
Factory for object creation abstraction, Strategy for runtime behavior selection, Decorator for adding responsibilities dynamically, Observer for event-driven architectures.
What is the advantage of the Builder pattern over the constructor
Builder enables readable multi-parameter object construction, fluent APIs, immutability, and optional parameters; cleaner than telescoping constructors or setters.
Whether you use the Comand pattern in work
Yes, used Command pattern for undo/redo operations, transaction queueing, and decoupling request senders from handlers in event-driven systems.
Why do you need a pattern Command
Command pattern encapsulates requests as objects, enabling queuing, logging, undo/redo, and separation of concerns between invoker and executor.
Which structure acts as quickly as possible to the Comand pattern than it can be replaced
Strategy pattern can replace Command when you need runtime algorithm selection without request encapsulation, though both solve behavioral flexibility differently.
Tell me something about microservice interaction
Microservices communicate via REST/HTTP, async messaging (RabbitMQ, Kafka), or gRPC depending on latency requirements, consistency needs, and topology.
What are the pluses of microservices before the monolith
Microservices provide independent scalability, technology diversity per service, fault isolation, and faster deployment cycles compared to monolithic architectures.
As if organized the interaction of several services
Organized interaction through API gateways for routing, service discovery (Eureka, Consul), event buses for async flows, and circuit breakers for resilience.
What synchronization channels used to organize the interaction of several services
Used async messaging (Kafka, RabbitMQ) for eventual consistency, REST for synchronous calls where ordering matters, and event sourcing for audit trails.
What design patterns do you use
Dependency Injection, Factory, Strategy, Observer, Circuit Breaker, and Saga patterns for coordinating distributed transactions across microservices.
Tell me about your design experience
Designed multi-tenant SaaS platforms and event-driven architectures with 100+ microservices; focused on minimizing coupling through clear contracts and async messaging.
What are design patterns
Design patterns are reusable solutions to common problems in software design that provide templates for writing maintainable, scalable code. They represent best practices and help developers communicate solutions using standardized vocabulary.
What is the lack of patterns
Lack of patterns leads to code duplication, inconsistent architecture, and harder maintenance. Without patterns, developers reinvent solutions repeatedly, increasing bugs and development time.
What is the difference between the Builder and Facade design template
Builder is a creational pattern that constructs complex objects step-by-step with fluent API, while Facade is a structural pattern that provides simplified interface to complex subsystems. Builder focuses on object creation; Facade focuses on simplifying interaction with existing components.
Knowing the answers is half the battle
The other half is explaining them clearly under pressure.
Try a free mock interviewarrow_forward