When we develop ASP, we often use if...else ... Or use switch to make multiple conditional judgments. As the following this user control (UserControl) using event Ver2 http://www.cnblogs.com/insus/archive/2011/11/18/2253605.html, is a piece of code:
In fact, according to the above writing a bit of a problem, but we see this code in the review code seems to take some time to read, the intention is not clear, the coupling is also high.
It is recommended that each case block be written as a separate method. It is then called for the purpose of minimizing the method.
In this way, the program can be changed to:
Modify here, we need to review the design mode of the intermediary (mediator) mode, more examples can refer to the "Design mode-intermediary (mediator) mode" http://www.cnblogs.com/insus/p/4134383.html and " Development (ASP. NET program) write code to the most object-oriented flavor "http://www.cnblogs.com/insus/p/4139346.html. In other words, when there are multiple if or switch, we can use the mediator design pattern to solve, the white is replaced.
Below we create a broker:
Once written, we can refactor the switch program:
Perform the demo in real time and get the exact same result:
This article has told us that the program is not written at once, but after the reconstruction will be written well, the house is built after the blank room, it is necessary to decorate, insus.net the understanding of the reconstruction, is not much change in the original nature, to do some changes, suitable for comfortable. Refactoring is often carried out with the idea of a design pattern.
This demo source program can be downloaded from the following link:
Http://download.cnblogs.com/insus/Refactoring/fact_switch_mediator_pattern.rar
Refactoring If...else ... or switch program block