代码改变世界

表驱动法1

2014-05-24 09:18  ttylinux  阅读(240)  评论(0编辑  收藏  举报

   这算是对表驱动法的内容阅读之后的总结。

 

需求:

读取用户输入的一个字符,如果该字符是属于字母、数字、标点符号这三类,则输出对应的提示信息(您输入的是数字,您输入的是字母,您输入的是标点符号);如果,输入的

字符非上述三类中的一种,则提示,您输入的是未知字符。

 

不使用表驱动法:

public class NormalWay {

	public boolean tellPunctuation(char c) {
		if (c == '.' || c == ',' || c == '?' || c == '!' || c == ';') {
			return true;
		} else {
			return false;
		}

	}

	public boolean tellAlphabet(char c) {

		if ((int) c >= 'a' && (int) c <= 'z') {
			return true;
		}

		if ((int) c >= 'A' && (int) c <= 'Z') {
			return true;
		}

		return false;
	}

	public boolean tellDigit(char c) {
		if ((int) c >= '0' && (int) c <= '9') {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		NormalWay one = new NormalWay();

		if (args.length > 0) {
			char oneChar = args[0].charAt(0);

			if (one.tellDigit(oneChar)) {
				System.out.println("Your input character is a digit.");
			} else if (one.tellAlphabet(oneChar)) {
				System.out.println("Your input character is a letter.");
			} else if (one.tellPunctuation(oneChar)) {
				System.out.println("Your input character is a punctuation.");
			} else {
				System.out.println("Your input character is unknown.");
			}
		}

	}
}

缺点就是,当我要判断某种输入是属于哪些类型时,那么,我要写很多的逻辑判断。

 

使用表驱动法:

使用表驱动法的关键就是,输入的参数是一个Key,然后这个Key是与某个值关联的。至于如何关联,复杂的呢,则会输入该Key,然后经过简单的计算,得出值;

简单的呢,则是直接根据Key,不用计算,直接得到对应的值。

public class SimpleTableDriven {

	private enum CharacterType {

		Digit, Letter, Punctuation, Unknown
	}

	private HashMap<Character, CharacterType> table = new HashMap<Character, CharacterType>();

	private void initalElementRelationship(
			HashMap<Character, CharacterType> table) {
		table.put('.', CharacterType.Punctuation);
		table.put(';', CharacterType.Punctuation);
		table.put('?', CharacterType.Punctuation);
		table.put('!', CharacterType.Punctuation);
		table.put(',', CharacterType.Punctuation);

		for (int i = 0; i < 26; i++) {
			int oneChar = (int) 'a' + i;
			table.put((char) oneChar, CharacterType.Letter);
		}

		for (int i = 0; i < 10; i++) {
			table.put((char) i, CharacterType.Digit);
		}
	}

	public CharacterType tellCharacterType(char c) {
		initalElementRelationship(this.table);

		if (!table.containsKey(c)) {
			return CharacterType.Unknown;
		} else {
			return table.get(c);
		}
	}

	public static void main(String[] args) {

		char oneChar = args[0].charAt(0);
		SimpleTableDriven oneTable = new SimpleTableDriven();
		if (CharacterType.Digit == oneTable.tellCharacterType(oneChar)) {
			System.out.println("Your input character is a digit.");
		} else if (CharacterType.Letter == oneTable.tellCharacterType(oneChar)) {
			System.out.println("Your input character is a letter.");
		} else if (CharacterType.Punctuation == oneTable
				.tellCharacterType(oneChar)) {
			System.out.println("Your input character is a punctuation.");
		} else if (CharacterType.Unknown == oneTable.tellCharacterType(oneChar)) {
			System.out.println("Your input character is unknown.");
		} else {
			System.out.println("Error.");

		}

	}
}

将确定输入字符的类型的过程,从通过逻辑判断来确定是什么类型,转变为通过查表的方式的方式来确定是什么类型。

好处是:

1.不用写一长串的逻辑判断。逻辑判断写得越多,就越容易出错。

2.要修改关系时,只需要在一个地方修改,在建立字符与类型的对应关系的地方修改,方便修改。

3.容易扩展

比如,我要将$也归为标点符号,那么,我只需要再添加一个关系便可table.put('$', CharacterType.Punctuation);而如果使用逻辑判断的话,那么我要在一长串的逻辑判断中,再添加一个逻辑判断。