CS FundamentalsHardProduct-company design interview
A vending machine's behaviour for insertCoin() and selectItem() differs completely in the states Idle, HasCoin, Dispensing and SoldOut, and the machine moves between these states itself. Which pattern models this, and how does it differ from Strategy?
- A. Strategy; the client swaps the algorithm, exactly as here
- B. State; the object switches its own state objects as events occur, while in Strategy the client usually picks the algorithm
- C. Template Method; each state overrides a step of the algorithm
- D. Command; each state is a request object
Correct answer: B. State; the object switches its own state objects as events occur, while in Strategy the client usually picks the algorithm
State and Strategy have the same class shape (delegation to an interchangeable object), but in State the transitions are driven internally by the context and the states often know about each other. Strategy is chosen from outside and the strategies are independent.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A report class defines generate() as: fetchData(); format(); export(). Subclasses PdfReport and CsvReport override only format() and…
- An app must create a consistent family of UI widgets (Button, Checkbox, Menu) for either a Light or a Dark theme, never mixing a Light…
- A game renders 1,000,000 trees. Each tree object stores mesh and texture data (about 2 MB, the same for every oak) plus its own x, y…
- An editor must save snapshots of a document's internal state so it can be restored later, without exposing the document's private fields…
- In an air-traffic system, 20 aircraft objects would each need references to all the others to coordinate landings. Routing every message…
- How many of these differences between Proxy and Decorator are correct? 1. Both implement the same interface as the object they wrap. 2. A…