Observer and Strategy are important Behavioral Design Patterns in Java. These patterns are frequently asked in interviews because they solve problems related to event notifications, dynamic behaviour switching, and clean software architecture.
Q1. Which of the following best describes the primary purpose of the Observer Pattern?
Options:
- A) To define a one-to-one dependency between objects
- B) To allow an object to be notified when another object changes state
- C) To define the steps involved in a process in a single class
- D) To allow a class to instantiate objects from a family of related classes
Answer:
B
Explanation:
- A (Incorrect): The Observer Pattern defines a one-to-many dependency.
- B (Correct): The Observer Pattern enables an object (subject) to notify other dependent objects (observers) when its state changes.
- C (Incorrect): This describes the Template Method Pattern, not Observer.
- D (Incorrect): This describes the Abstract Factory Pattern, not Observer.
Q2. An e-commerce platform updates all registered users via email whenever a product’s price changes. Which design pattern is most suitable for this scenario?
Options:
- A) Singleton Pattern
- B) Observer Pattern
- C) Factory Pattern
- D) Composite Pattern
Answer:
B
Explanation:
- A (Incorrect): Singleton ensures a class has only one instance, which is unrelated to notification.
- B (Correct): The Observer Pattern is ideal for notifying multiple users (observers) when the product price (subject) changes.
- C (Incorrect): Factory is for object creation, not notification.
- D (Incorrect): Composite manages hierarchical object structures, not notifications.
Q3. An e-commerce site needs to apply different discount approaches based on membership type, like VIP or regular customer. Which pattern best fits to dynamically switch between these approaches?
Options:
- A) Strategy Pattern
- B) Adapter Pattern
- C) Flyweight Pattern
- D) Proxy Pattern
Answer:
A) Strategy Pattern
Explanation:
- (A) Strategy Pattern: Correct. Strategy enables dynamic switching between discount approaches.
- (B) Adapter Pattern: Incorrect. Adapter converts interfaces, not intended for choosing approaches.
- (C) Flyweight Pattern: Incorrect. Flyweight reduces memory usage but doesn’t handle approaches.
- (D) Proxy Pattern: Incorrect. Proxy controls access rather than dynamically selecting approaches.
Q4. What is the primary purpose of the Strategy Pattern?
Options:
- A) To provide a way to extend classes in a structured way.
- B) To define a family of algorithms and make them interchangeable at runtime.
- C) To optimize code by reducing the number of classes.
- D) To allow only one instance of a class.
Answer:
B
Explanation:
- Option A: Incorrect. The Strategy Pattern is not focused on extending classes or inheritance structures but on providing interchangeable behaviours, each represented as a separate class implementing a specific strategy interface.
- Option B: Correct. The Strategy Pattern defines a family of algorithms, each encapsulated in its own class, and makes it possible to switch between them at runtime without changing the client code. This flexibility allows different implementations to be chosen based on dynamic conditions.
- Option C: Incorrect. The Strategy Pattern may lead to additional classes, as each strategy typically requires its own concrete implementation. Its goal is not to reduce the number of classes but to allow various algorithms to be selected and used interchangeably.
- Option D: Incorrect. Limiting class instances is the purpose of the Singleton Pattern, not the Strategy Pattern. The Strategy Pattern focuses on dynamically changing behaviour rather than enforcing a single instance.
Why These Patterns Matter in Java Interviews
Interviewers ask these patterns to test:
- Event-driven design
- One-to-many relationships
- Notification systems
- Dynamic algorithm switching
- Clean architecture
- Flexible coding practices
- Real-world problem solving
Common Real-World Examples
Observer Pattern:
- Email notifications
- Stock price alerts
- News subscriptions
- UI event listeners
Strategy Pattern:
- Payment method selection
- Discount engines
- Sorting algorithms
- Shipping charge calculators
Quick Revision
- Observer notifies dependent objects when state changes.
- Strategy switches algorithms dynamically at runtime.
- Both are key Behavioral Design Patterns in Java.
Visited 5 times, 5 visit(s) today

