Java

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.

bar_chartQuick stats
Total questions48
High frequency2
With code examples3
1

What design patterns do you know

Common patterns: Singleton, Factory, Strategy, Observer, Adapter, Decorator, Proxy, Builder, Template Method, and State.

java
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();
  }
}
2

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.

java
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();
3

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.

java
class Singleton {
  private static Singleton instance;
  private Singleton() {
  }
  public static Singleton getInstance() {
    if(instance == null) instance = new Singleton();
    return instance;
  }
}
4

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.

5

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.

6

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.

7

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.

8

What are design patterns

Reusable solutions to common design problems: creational (Builder, Singleton), structural (Adapter, Decorator), behavioral (Strategy, Observer).

9

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.

10

What design patterns do you use

Singleton, Factory, Builder, Strategy, Decorator, Observer, and Repository patterns depending on use-case requirements.

11

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.

12

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.

13

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.

14

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.

15

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.

16

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.

17

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.

18

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.

19

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.

20

Why do you need a pattern Command

Command pattern encapsulates requests as objects, enabling queuing, logging, undo/redo functionality, and decoupling invoker from receiver.

21

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.

22

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.

23

What patterns used besides Singleton

Common patterns: Factory, Strategy, Observer, Decorator, Adapter, Template Method, Proxy, Chain of Responsibility, and Dependency Injection patterns.

24

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.

25

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.

26

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.

27

Types of design templates

Design pattern types: Creational (object creation), Structural (object composition and relationships), Behavioral (communication between objects and responsibility distribution).

28

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).

29

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).

30

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).

31

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).

32

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.

33

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.

34

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.

35

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.

36

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.

37

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.

38

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.

39

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.

40

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.

41

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.

42

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.

43

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.

44

What design patterns do you use

Dependency Injection, Factory, Strategy, Observer, Circuit Breaker, and Saga patterns for coordinating distributed transactions across microservices.

45

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.

46

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.

47

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.

48

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

More Java topics