dr. evil
dr. evil

Reputation: 27265

Which design pattern to do this?

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

  1. Prepare
  2. Eat
    • Take your stuff to bin
    • 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.)

Upvotes: 4

Views: 12632

Answers (5)

MRFerocius
MRFerocius

Reputation: 5629

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

Upvotes: 3

Noldorin
Noldorin

Reputation: 147340

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.

Upvotes: 2

egaga
egaga

Reputation: 21732

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.

Upvotes: 8

Matt Hinze
Matt Hinze

Reputation: 13679

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

Upvotes: 13

Paul Hollingsworth
Paul Hollingsworth

Reputation: 13374

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.)

Upvotes: 3

Related Questions