Table-Driven Approach - a more elegant form of if-else or switch-case

2023/12/21 design-pattern 共 485 字,约 2 分钟
程序员

Table-Driven Approach

  1. Direct Table Access Calculate the number of days in each month.
    if(month == 1) {
     days = 31;
    } else if (month = 2){
     days = 28;
    } else if (month = 3){
     days = 31;
    } else if (month = 4){
     days = 30;
    } else if (month = 5){
     days = 31;
    } else if (month = 6){
     days = 30;
    } else if (month = 7){
     days = 31;
    } else if (month = 8){
     days = 31;
    } else if (month = 9){
     days = 30;
    } else if (month = 10){
     days = 31;
    } else if (month = 11){
     days = 30;
    } else if (month = 12){
     days = 31;
    }
    

    Save this data into a table and create this table:

[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
charType = charTypeTable[inputChar];

Document

Search

    Table of Contents