获得月的天数是典型的直接访问驱动表方式的实例,下面我们来展示一下:
MonthDaysTest.java
package com.study.test; import org.junit.Assert; import org.junit.Test; import com.study.MonthDays; public class MonthDaysTest { @Test public void test_when_given_month_number_return_month_days() { MonthDays monthDays = new MonthDays(); Assert.assertEquals(31, monthDays.getTotalDayInMonth(1)); Assert.assertEquals(28, monthDays.getTotalDayInMonth(2)); Assert.assertEquals(31, monthDays.getTotalDayInMonth(3)); Assert.assertEquals(30, monthDays.getTotalDayInMonth(4)); Assert.assertEquals(31, monthDays.getTotalDayInMonth(5)); Assert.assertEquals(30, monthDays.getTotalDayInMonth(6)); Assert.assertEquals(31, monthDays.getTotalDayInMonth(7)); Assert.assertEquals(31, monthDays.getTotalDayInMonth(8)); Assert.assertEquals(30, monthDays.getTotalDayInMonth(9)); Assert.assertEquals(31, monthDays.getTotalDayInMonth(10)); Assert.assertEquals(30, monthDays.getTotalDayInMonth(11)); Assert.assertEquals(31, monthDays.getTotalDayInMonth(12)); } @Test public void test_when_given_month_number_return_month_days_by_table() { MonthDays monthDays = new MonthDays(); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(1)); Assert.assertEquals(28, monthDays.getTotalDayInMonthFromTable(2)); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(3)); Assert.assertEquals(30, monthDays.getTotalDayInMonthFromTable(4)); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(5)); Assert.assertEquals(30, monthDays.getTotalDayInMonthFromTable(6)); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(7)); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(8)); Assert.assertEquals(30, monthDays.getTotalDayInMonthFromTable(9)); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(10)); Assert.assertEquals(30, monthDays.getTotalDayInMonthFromTable(11)); Assert.assertEquals(31, monthDays.getTotalDayInMonthFromTable(12)); } }
MonthDays.java
package com.study; public class MonthDays { /* * if...else.. * 获得某一月中的总天数,monthIndex从 1 开始 */ public int getTotalDayInMonth(int month) { int totalDay = 0; if (month == 2) { totalDay = 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { totalDay = 30; } else { totalDay = 31; } return totalDay; } private int[] totalDayTable = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* * 表驱动法 * 获得某一月中的总天数,monthIndex从 1 开始 */ public int getTotalDayInMonthFromTable(int month) { return totalDayTable[month - 1]; } }
附采用表驱动法对实际代码进行重构实例:
package com.bijian.study; import java.util.HashMap; import java.util.Map; public class TableMethod { public void process(String state) { String needState; String needChangeResult; //原来的写法 if("COMPIETED".equals(state)) { needState = "COMPIETED"; needChangeResult = "APPROVE_OK"; }else if("REJECTED".equals(state)) { needState = "NEW"; needChangeResult = "APPROVE_FAIL"; }else if("ABANDON".equals(state)) { needState = "NEW"; needChangeResult = "APPROVE_FAIL"; } //表驱动法 Map<String, String[]> stateTable = new HashMap(); stateTable.put("COMPIETED", new String[]{"COMPIETED", "APPROVE_OK"}); stateTable.put("REJECTED", new String[]{"NEW", "APPROVE_FAIL"}); stateTable.put("ABANDON", new String[]{"NEW", "APPROVE_FAIL"}); needState = stateTable.get(state)[0]; needChangeResult = stateTable.get(state)[1]; } }