Senior
How is the description of transitions between statuses organized?
sobes.tech AI
Answer from AI
Transitions between statuses are usually organized using finite automata (state machines) or the "State" pattern. In Java, this can be implemented using an enum, where each status knows which statuses it can transition to. For example:
public enum Status {
NEW(EnumSet.of(IN_PROGRESS, CANCELLED)),
IN_PROGRESS(EnumSet.of(DONE, CANCELLED)),
DONE(EnumSet.noneOf(Status.class)),
CANCELLED(EnumSet.noneOf(Status.class));
private final Set<Status> allowedTransitions;
Status(Set<Status> allowedTransitions) {
this.allowedTransitions = allowedTransitions;
}
public boolean canTransitionTo(Status target) {
return allowedTransitions.contains(target);
}
}
This approach allows centralized control of transition logic and easy extension. In more complex cases, libraries for state management (e.g., Spring State Machine) are used.