当前位置:网站首页>How to solve too many if statements

How to solve too many if statements

2022-07-23 12:47:00 liangdu_ Zuker

How to solve if Too many sentences

  • 【 Use strategic mode 】 Define a policy base class , Then for each if Implement a policy subclass , Then use the parameters passed in , To match the adopted strategy , It's sort of like the idea of divide and rule .

  • 【 Use sentinel pseudo nodes 】 We usually use if To judge the boundary problem , For non boundary problems, there is no need to use if, Then I just need to add the previous and subsequent sentinel pseudo nodes to the current boundary problem , You can do it without using if Statement .

  • 【 Use drive table mode 】, Create a actionmap,key That's our condition case, value It's for this case Corresponding processing function handle. This can also avoid writing a lot if .

  • 【 Use the responsibility chain model 】, namely , Will be multiple if, Divide into n Group if, every last if The group is encapsulated into a responsibility chain node , Then pass the parameters to the responsibility chain , The nodes of the responsibility chain pass judgment . With this layer of encapsulation of the responsibility chain , There won't be a bunch of hard to read if Statements are stacked .

原网站

版权声明
本文为[liangdu_ Zuker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230540150291.html