Objective
The State pattern represents each state as a class that knows which other states can be reached. That way the valid transitions are defined in one place, instead of being spread in conditions all over the program.
Instructions
- An order starts as
new. The actions arepay(new → paid),ship(paid → shipped),deliver(shipped → delivered) andcancel(from new or paid → cancelled). - The first line has the number
nof actions; then one per line. ShowState: new stateif it is valid, orInvalid transition: action from stateif it is not (the state does not change).
3
pay
ship
deliver
State: paid
State: shipped
State: delivered
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise