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.
if you are looking for design pattern. I think it's Chain-of-responsibility pattern
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" );
}
精彩评论