Command, State, and Visitor are important Behavioral Design Patterns in Java. These patterns are frequently asked in interviews because they solve problems related to request encapsulation, state-based behaviour changes, and adding operations without modifying classes.
Q1. What is the primary purpose of the Command Pattern?
Options:
- A) To encapsulate commands as objects, allowing them to be parameterized
- B) To enforce direct communication between objects for high performance
- C) To enable classes to inherit behaviour from multiple parents
- D) To facilitate automatic dependency injection across objects
Answer:
A
Explanation:
- A is correct because the Command Pattern encapsulates a request as an object, allowing it to be parameterized and used flexibly.
- B is incorrect as the pattern does not focus on direct communication between objects.
- C is incorrect since inheritance is unrelated to the Command Pattern.
- D is incorrect because dependency injection is not a primary concern of this pattern.
Q2. In which scenarios would the Command Pattern be most beneficial? (Select all that apply)
Options:
- A) Implementing an undo/redo system for a text editor
- B) Building a direct-messaging feature for a social media app
- C) Developing a menu system with various operations in a graphical interface
- D) Creating a singleton service for database connections
Answer:
A and C
Explanation:
- A and C are correct because the Command Pattern is effective for implementing undo/redo functionalities and encapsulating operations in menu systems.
- B is incorrect since a direct-messaging feature does not require command encapsulation.
- D is incorrect as it pertains to singleton usage rather than command encapsulation.
Q3. What is the primary purpose of the State Pattern in object-oriented design?
Options:
- A) To encapsulate multiple states within a single object
- B) To separate behaviour based on an object’s state into different classes
- C) To store the history of state changes in an application
- D) To enable communication between objects in different states
Answer:
B
Explanation:
- B is correct because the State Pattern allows behaviour to vary based on an object’s state by delegating to different classes for each state.
- A is incorrect as it doesn’t encapsulate multiple states within a single object; rather, it dynamically changes behaviour.
- C is incorrect since tracking state history is not the main purpose of the State Pattern.
- D is incorrect as inter-object communication isn’t typically part of this pattern’s goal.
Q4. A media player application has different states: Playing, Paused, and Stopped. Which design pattern would best manage transitions between these states?
Options:
- A) Observer Pattern
- B) State Pattern
- C) Command Pattern
- D) Factory Pattern
Answer:
B
Explanation:
- B is correct because the State Pattern is designed to handle an object’s behaviour based on its current state, such as “Playing,” “Paused,” or “Stopped.”
- A, C, and D are incorrect as they don’t focus on managing state transitions and state-based behaviour.
Q5. What is the primary purpose of the Visitor Pattern?
Options:
- A) To simplify state transitions between objects
- B) To define a new operation on an object without changing its class
- C) To create a single class that handles multiple requests
- D) To enforce strict access control within an object structure
Answer:
B
Explanation:
- B is correct because the Visitor Pattern allows adding new operations to classes without modifying them.
- A, C, and D are incorrect as they do not describe the purpose of the Visitor Pattern.
Q6. A company has an employee hierarchy with roles such as Manager, Engineer, and Intern. It needs to perform various reports on these roles, such as performance analysis and salary calculation. Why might the Visitor Pattern be beneficial here? (Select all that apply)
Options:
- A) It allows adding new reporting functionalities without modifying existing role classes
- B) It centralizes role-specific data and makes it easier to manage roles
- C) It encourages the separation of roles and operations on them
- D) It enforces role-based security and access controls
Answer:
A and C
Explanation:
- A and C are correct because the Visitor Pattern allows adding new operations (e.g., performance analysis) without altering the existing role classes, adhering to open/closed principle.
- B and D are incorrect as they do not represent the pattern’s focus on operations over structure.
Why These Patterns Matter in Java Interviews
Interviewers ask these patterns to test:
- Request encapsulation
- Undo/redo systems
- State-driven behaviour
- Object responsibility separation
- Open/closed principle
- Extensible design
- Real-world architecture skills
Common Real-World Examples
Command Pattern:
- Undo/redo editors
- Toolbar actions
- Task queues
- Remote controls
State Pattern:
- Media players
- ATM machines
- Order status systems
- Traffic lights
Visitor Pattern:
- Payroll reports
- Performance analytics
- AST compilers
- Document processors
Quick Revision
- Command wraps requests into objects.
- State changes behaviour based on current state.
- Visitor adds new operations without changing classes.
- All three are major Behavioral Design Patterns in Java.
Visited 6 times, 6 visit(s) today

