Refactoring If...else ... or switch program block

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.