表驅動開發優點

來源:互聯網
上載者:User
表驅動方法2009-08-01 13:46
表驅動方法

1:什麼是表驅動法.
     表驅動法是一種編程模式(Scheme),從表裡面尋找資訊而不使用邏輯語句(if 和case)它的好處是消除代碼裡面到處出現的if、else、swith語句,讓淩亂代碼變得簡明和清晰。對簡單情況而言,表驅動方法可能僅僅使邏輯語句更容易和直白,但隨著邏輯的越來越複雜,表驅動法就愈發有吸引力。
2:表驅動法的例子示範
    假設有段程式要計算某年某月的天數
    通常的做法如下:

private void btnCalculate_Click(object sender, EventArgs e)
        {
            //檢測輸入是否正確
            if (!CheckInput()) return;
           
            int days = 0 ;

            int month = Convert.ToInt16(txbMoth.Text);

            switch (month)
            {
              case 1:
                  days = 31;
                  break;
              case 2:
                  if (IsLeapYear(txbYear.Text))
                  {
                      days = 29;
                  }
                  else
                  {
                      days = 28;
                  }
                  break;
              case 3:
                  days = 31;
                  break;
                 case 4:
                  days = 30;
                  .....                  .....              case 11:
                  days = 30;
                  break;
              case 12:
                  days = 31;
                  break;
              default:
                  break;
            }
           txbOutPut.Text = days.ToString();                           }

大家可能會看到這裡會出現大量的switch、case語句,其實這隻是個簡單的邏輯,如果在商務邏輯複雜的情況下,代碼裡
這些if 、else,switch,case語句必將是鋪天蓋地的出現。好了,我們來看看,表驅動的方法的簡單應用吧
 private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;

        
            int[] dayPerMonth = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            int[] ldayPerMonth = new int[12] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

            int days = 0;
            int index = Convert.ToInt16(txbMoth.Text);

            if (IsLeapYear(txbYear.Text))
            {
                days = ldayPerMonth[index];
            }
            else
            {
                days = dayPerMonth[index];
            }

            txbOutPut.Text = days.ToString(); 
           
          
        }

對比這兩段代碼,你會發現如果用表驅動法,你的代碼將會更簡潔,明了。
3:表驅動法查詢資料的方式

    * 直接存取(Direct Access)
    * 索引訪問(Index Access)
    * 階梯訪問(Stair-Step Access)
    有時需要進行變址查詢4:表驅動法優點
     前面一直在強調錶驅動法的優點,下面我們來總結一下:

    * 在適當環境下,使用它能夠使代碼簡單、明了。
    * 修改容易(易維護)、效率更高。
    * 表驅動法的一個好處就是能夠大量消除代碼中if else, swith 判斷。
       

代碼大全上的,提倡用此法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.