Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A design pattern for switching, or cycling, between statuses

Is there a design pattern (like visitor, strategy, state, etc.), or some other design principle, for helping to design a good solution for simulating a flow in states for an entity, for example a Task entity.

A Task starts in New status, then progresses to BeingHandled status, then WaitingForApproval and then it can either be moved to Finished or NotApproved, which is essentially back to BeingHandled with additional info, saying that it is back from WaitingForApproval.

So in general we have some general flow, and then we can have some inner flow within it.

Thanks,

ashilon

like image 506
ashilon Avatar asked Jan 08 '23 05:01

ashilon


1 Answers

It's not a pattern but a concept: finite-state machine.

In summary, it's a state machine which can only have an active state in a moment in time.

Check what Wikipedia article says on its first paragraph:

A finite-state machine (FSM) or finite-state automaton (plural: automata), or simply a state machine, is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is conceived as an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. A particular FSM is defined by a list of its states, and the triggering condition for each transition.

like image 162
Matías Fidemraizer Avatar answered Jan 10 '23 20:01

Matías Fidemraizer