I have an object which represents some document stored in a relational database (O/R mapping). This document has a status field which identifies its state. Every status (ie. entry, wait for approval, sent, paid etc…) carries it’s own validation rules and requirements.
I’m wondering what is a correct OO implementation of such workflow. The easiest way of doing it is placing a bunch of if statements like
if (status == something || status == something else)
check if the date can be changed();
but in a complex scenario this becomes extremely hard to read.
Suggestions for good design?
Have a look at the
Strategy Pattern. For each possible state you would create a class implementing the steps to perform.Maybe you want to combine it with a Factory that builds the correct strategy object depending on the state of the doucment when it is read from the DB.