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?
- A. State for the vending machine; Strategy for the route planner
- B. Strategy for both
- C. State for the route planner; Strategy for the vending machine
- 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
- Account.withdraw(amount) works for any amount up to the balance. A subclass FixedDeposit overrides it to throw an exception unless the…
- Which call violates the Law of Demeter (talk only to your immediate collaborators) inside an OrderService method that receives an Order…
- How many of these statements about Java methods are correct? 1. A final method can be overloaded in a subclass. 2. A static method can be…
- Classes A, B extends A and C extends B all override m(). Inside C, which way can call A's version of m() on the current object?
- Joshua Bloch recommends implementing a singleton as a single-element enum. Which advantage does this give over a class with a private…
- What is printed? cpp struct P { const char s; P(const char s) : s(s) { cout << s; } ~P() { cout << "~" << s; } }; struct B { P b{"B"}; }…