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

前端 未结 4 908
臣服心动
臣服心动 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:35

    Can't tell if Chain of Responsibility is your answer, or even if GoF applies. Visitor might be the right thing. Not enough information to be certain.

    It could be that your problem can be handled with good old-fashioned polymorphism. Or maybe a Map that used keys to pick out the appropriate handler object.

    Keep 'do the simplest thing possible' in mind. Don't leap to the complex until you've proven to yourself that you need it.

    I recently read about the Anti-IF campaign that promotes this idea. Sounds quite pertinent here.

提交回复
热议问题