Dealing with nested if then else/nested switch statements

前端 未结 7 1567
走了就别回头了
走了就别回头了 2020-12-30 08:43

Are there any design patterns/methods/ways to remove nested if then else conditions/switch statements?

I remember coming across some methods used by the Google folks

7条回答
  •  遥遥无期
    2020-12-30 08:49

    You might want to look at the Strategy Pattern, wherein instead of putting a long chain of ifs with linked conditions, you abstract each condition to a different object, each one defining what its specific behavior.

    The class that defines those objects will implement an Interface which will be called by the parent object.

提交回复
热议问题