表驱动法实例
编程技术  /  houtizong 发布于 3年前   67
获得月的天数是典型的直接访问驱动表方式的实例,下面我们来展示一下:
MonthDaysTest.java
package com.study.test;import org.junit.Assert;import org.junit.Test;import com.study.MonthDays;public class MonthDaysTest {@Testpublic 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));}@Testpublic 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];}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接