What are the advantages of chain-of-responsibility vs. lists of classes?

前端 未结 4 923
臣服心动
臣服心动 2020-12-06 10:03

Recently, I was discussing with another programmer the best way to refactor a huge(1000 lines) method full of \"if\" statements.

The code is written in Java, but I g

4条回答
  •  醉话见心
    2020-12-06 10:32

    Go for CoR, if One of below satisfies:

    • Your handlers depend on each other.
    • You do not need parallelism between your handlers.
    • You need ordering between these handlers
    • If you want to return from say, the intermediate handler and do not want to process the rest of the handlers. It makes much more sense to use CoR, as you do not need to write ifs inside for loop. You can instead just return from the current chain instead of going next in the chain.

提交回复
热议问题