当前位置:首页 > 开发 > 研发管理 > 正文

表驱动法实例

发表于: 2014-09-24   作者:bijian1013   来源:转载   浏览:
摘要: 获得月的天数是典型的直接访问驱动表方式的实例,下面我们来展示一下: MonthDaysTest.java package com.study.test; import org.junit.Assert; import org.junit.Test; import com.study.MonthDays; public class MonthDaysTest { @T

获得月的天数是典型的直接访问驱动表方式的实例,下面我们来展示一下:

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];
	}
}

表驱动法实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
  这本书讲什么? code complete 是编码完成的意思,是一个软件项目开发过程中的重要里程碑(miles
本文出自:http://blog.csdn.net/svitter DFA: 使用了表驱动法; 构造的表如下: 表驱动 num . E +
本文出自:http://blog.csdn.net/svitter DFA: 使用了表驱动法; 构造的表如下: 表驱动 num . E +
一. 概念   判定表是分析和表达多逻辑条件下执行不同操作的情况的工具。 二. 判定表驱动法的应用
本文的一些基本概念参考了一部分百度百科,当然只保留了最有价值的部分,代码部分完全是自己实现!
说明:本处的素数判断函数,只适用于int型。在无符号int和int64上的正确性还没有进行严格的验证。
嵌入式Linux之我行,主要讲述和总结了本人在学习嵌入式linux中的每个步骤。一为总结经验,二希望能
S3C2410 串口硬件描述 S3C2410 内部具有 3 个独立的 UART 控制器,每个控制器都可以工作在 Interrupt
1.需要更改为横表的纵表如下图所示横表要求结构为busi_code为表的各个信息显示字段名称: 2.更改横表
1. 总体设计思想 整个词法分析器有一张预测分析表,一个先进后出的分析栈和一个总控程序组成。主控
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号