I'm about to implement my first State Machine and I'm left with one question, which is the acceptable coding form on who should change the state of the object/entity? Should the StateMachine change the state? Should the object itself change its own state? Should a sevice (e.g. IncidentTicketService) change the state of the entity (IncidentTicket) ?
Concrete example
public enum IncidentState {
OPEN, CLOSED, CANCELLED, PENDING
}
...
public class IncidentTicket {
private String state;
}
...
public IncidentTicketStateMachine {
// code here depends if this object is doing the state change, or
// just indicating which are the next/previous states based on the
// provided object in a constructor... I guess.
}
...
# only if this is common practice to change the state of the object
public class IncidentService {
private IncidentTicket incidentTicket;
public IncidentService(IncidentTicket incidentTicket) {
this.incidentTicket = incidentTicket;
}
public void nextState() {
this.incidentTicket.setStatus(....);
}
...
I don't mind coding it either way (object changes state vs state machine), I'm asking since I want to find out which of the two is most common?
Thanks