Given that this question is related to my work, I will simplify what I need to do. The system consists of 4 primary types of statuses (internally, we call them events):
- Working, not operating machinery.
- Working, operating machinery.
- Break period.
- Off work.
Based on those types of events I have to keep 4 different counters with:
- A counter for total work hours (counter 1).
- A counter for work hours of operating machinery (counter 2).
- A counter for tracking break periods (counter 3)
- A counter for off work hours (counter 4).
In principle, this looks very simple, but the counters can have very complex rules of how to calculate the hours (i.e, "break period" is only considered as such if the worker was on that status for more than 40 minutes, otherwise it should be considered as "working not operating machinery"). In addition, there can be new event types which mix features of other events (i.e., "break period eating ice cream" [new event type] or "working while being off-work" [yep, it doesn't make sense]).
The current solution is a canonical example of spaghetti code: the business rules are defined in a tangled mess of if/then/else statements. It has been declared un-maintainable. That is why I have been tasked with implementing a new solution.
What I have thought is to implement the status events as a class hierarchy so it is easy to mix functionality of different types of events (i.e., a new event type that inherits from "break period" and "off work"). However, I haven't been able to think of an elegant and easy-to-extend way to process the rules of each counter. I don't want to end up with a long sequence of if-else statements.
My goal is to achieve a solution which, later on, can support new type of events and the counters will do the right thing. For instance, a new event "Break period while working not operating machinery" is added, and counters 2 and 3 will take it into account without having to modify them.
What I want is some general design ideas of how this could be achieved in an elegant and extensible manner.