Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronizing two state machines

Say, I am building a business process management application. It has the following entities: issues and tasks, related to each other as 1 issue to many tasks. Both, task and issue, have their own states and the state of one may influence the state of another.

For example, both of them have "Cancelled" and "Completed" states. When I change the state of the issue to "Cancelled", all of its tasks should become "Cancelled". When I change the state of all tasks to "Completed", the issue should automatically become "Completed".

Supposing that there are quite a few states for both entities and the logic of transitions from one state to another and the dependency of states may change, are there any design patterns and/or best practices to handle that situation?

like image 259
artemb Avatar asked Jun 09 '09 04:06

artemb


1 Answers

The design pattern that leaps to mind is a "rule" ;-)

Or, if you prefer, a Command Pattern

In other words, for situations like this I would create a database table listing the states and acceptable transitions, and associate an action with each transition (using reflection)

I have found this to be useful to handle cases where the transition action is more complex than just updating the statuses to match.

For example, in one system we had a workflow where a request document had to pass through several committee review stations, each of which could reject or pass on the document to the next stage, plus custom side-effect procesing. The committee organization, processing structures, and processing actions changed significantly three times during development, and five more times in the first year of deployment.

like image 71
Steven A. Lowe Avatar answered Nov 15 '22 10:11

Steven A. Lowe