views:

138

answers:

5

I've got a process something like a workflow, assume it like this:

  1. Prepare
  2. Eat
  3. Take your stuff to bin
  4. Clean up the table

Now what I want to do is even the user cancels the "Eat" event I want them to "Clean up the table" same goes for "Prepare" and "Take your stuff to bin" stages.

Currently in my implementation I had to do several checks and sometimes I end up like calling "Clean up table" twice, and some other branching issues when I add couple of more steps.

Is there any well defined design pattern to deal with this kind of flows? (AFAIR there was one I just can't recall the name of it.)

+3  A: 

Sounds like Template method pattern.

Or you can do this via composition and Strategy pattern.

If you start to have complicated logic, then State pattern could be better.

egaga
+1  A: 

In C#/.NET, we have the IDisposable pattern. I'm sure you could implement something similar in another language, whether or not it has a garbage collector (though the implementation would differ slightly).

Regarding the workflow aspect of this, I would just follow the design pattern of web services in WCF (i.e. Begin and Cancel methods). If you don't consider it overkill for your circumstances, the Windows Workflow Foundation may be the best way to go.

Noldorin
actually IDisposable is not a bad idea, currently I kept a copy of the object even after the final step but I might refactor it.
dr. evil
+2  A: 

If you're working in C++: Resource Acquisition is Initialization.

This problem generally hasn't been solved well in languages without some sort of deterministic finalisation (C#, Java etc.)

Paul Hollingsworth
+7  A: 

This is solved with the State pattern. If you test drive the logic it'll go smoothly.

Matt Hinze
This was the one I was thinking of. Quite flexible for a workflow. Cheers.
dr. evil
A: 

State Pattern my firend is what will make this work at end.

Best Regards!

MRFerocius