开发者

"Procedural" Design Pattern for DoThis->ThenThis->AndThenThis

开发者 https://www.devze.com 2023-01-15 05:25 出处:网络
I\'m trying to find a useful design pattern that simulates something like this following: DoThis()->ThenThis()->FinallyDoThis() then print \"Hello world\"

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" );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消