ProgrammingHardProduct-company placement · GATE CS

A vending machine object behaves differently when Idle, HasCoin or Dispensing, and each state object decides which state comes next. A route planner lets the caller choose fastest, shortest or cheapest routing. Which patterns fit these two designs?

  1. A. State for the vending machine; Strategy for the route planner
  2. B. Strategy for both
  3. C. State for the route planner; Strategy for the vending machine
  4. D. Template Method for the vending machine; Observer for the route planner

Correct answer: A. State for the vending machine; Strategy for the route planner

Both delegate to an interchangeable object, but in State the objects represent the context's internal condition and drive transitions themselves. In Strategy the client picks an algorithm and the strategies know nothing of each other.

More OOP with C++ and Java questions

Exams that ask this topic