jeudi 9 juin 2016

How to avoid cross-checks

I have several jobs to run (pseudocode):

public bool IsJob1Running;
public void DoJob1(...) { IsJob1Running = true; ... ; IsJob1Running = false; }

public bool IsJob2Running;
public void DoJob2(...) { IsJob2Running = true; ... ; IsJob2Running = false; }

...

In some cases only single job can run at a time, in others - multiple can run, but others shouldn't run (should wait or refuse to start). All this at some point lead to monstrous checks like this:

if(!IsJob1Running && !IsJob2Running && !IsJob3Running ... ) { ... }

Those are suddenly appears everywhere in the software: when user click button (or even before to disable button), before starting job, even inside DoJob, etc.

I hate it. Imagine a case when new Job99 has to be added. Then everywhere in the software all checks has to be updated to include Job99 checks.

My question: is there a pattern existing to define this kind of cross-checks (relations?), which will allow to add new jobs easily, have centralized overview of all dependencies, etc.?

Aucun commentaire:

Enregistrer un commentaire