views:

72

answers:

2

I'm trying to find a useful design pattern that simulates something like this following:

DoThis()->ThenThis()->FinallyDoThis() then print "Hello world"

ThenThis() doesn't run unless DoThis() passes. And FinallyDoThis() won't run unless ThenThis() and DoThis() both pass. If all the methods methods pass, then it prints "Hello world".

Is there an existing design pattern that would fit nicely to this? I've been investigating the usage of monads, but not sure if they are applicable to this case.

+4  A: 

Without knowing your language, you could put it in an if statement if the language if statement short-circuits. Then have each function return true or false:

if( foo() && bar() && baz() ){
    System.out.println( "hello world" );
}
wheaties
+3  A: 

if you are looking for design pattern. I think it's Chain-of-responsibility pattern

Arseny
+1: except the chain continues until failure instead of continuing until success. But certainly similar implementation and concept.
Don Roby
Thank you for actually mentioning a design pattern which was the original request.
elmt