Chapter 18. Table-Driven Methods

cc2e.com/1865

Contents

Related Topics

A table-driven method is a scheme that allows you to look up information in a table rather than using logic statements (if and case) to figure it out. Virtually anything you can select with logic statements, you can select with tables instead. In simple cases, logic statements are easier and more direct. As the logic chain becomes more complex, tables become increasingly attractive.

If you're already familiar with table-driven methods, this chapter might be just a review. In that case, you might examine "Flexible-Message-Format Example" in Direct Access Tables for a good example of how an object-oriented design isn't necessarily better than any other kind of design just because it's object-oriented, and then you might move on to the discussion of general control issues in Chapter 19.

General Considerations in Using Table-Driven Methods

General Considerations in Using Table-Driven Methods

Used in appropriate circumstances, table-driven code is simpler than complicated logic, easier to modify, and more efficient. Suppose you wanted to classify characters into letters, punctuation marks, and digits; you might use a complicated chain of logic like this one:

Example 18-1. Java Example of Using Complicated Logic to Classify a Character

if ( ( ( 'a' <= inputChar ) && ( inputChar <= 'z' ) ) ||
   ( ( 'A' <= inputChar ) && ( inputChar <= 'Z' ) ) ) {
   charType = CharacterType.Letter;
}
else if ( ( inputChar == ' ' ) || ( inputChar == ',' ) ||
   ( inputChar == '.' ) || ( inputChar == '!' ) || ( inputChar == '(' ) ||
   ( inputChar == ')' ) || ( inputChar == ':' ) || ( inputChar == ';' ) ||
   ( inputChar == '?' ) || ( inputChar == '-' ) ) {
   charType = CharacterType.Punctuation;
}
else if ( ( '0' <= inputChar ) && ( inputChar <= '9' ) ) {
   charType = CharacterType.Digit;
}

If you used a lookup table instead, you'd store the type of each character in an array that's accessed by character code. The complicated code fragment just shown would be replaced by this:

Example 18-2. Java Example of Using a Lookup Table to Classify a Character

charType = charTypeTable[ inputChar ];

This fragment assumes that the charTypeTable array has been set up earlier. You put your program's knowledge into its data rather than into its logic—in the table instead of in the if tests.

Two Issues in Using Table-Driven Methods

Two Issues in Using Table-Driven Methods

When you use table-driven methods, you have to address two issues. First you have to address the question of how to look up entries in the table. You can use some data to access a table directly. If you need to classify data by month, for example, keying into a month table is straightforward. You can use an array with indexes 1 through 12.

Other data is too awkward to be used to look up a table entry directly. If you need to classify data by Social Security Number, for example, you can't use the Social Security Number to key into the table directly unless you can afford to store 999-99-9999 entries in your table. You're forced to use a more complicated approach. Here's a list of ways to look up an entry in a table:

  • Direct access

  • Indexed access

  • Stair-step access

Each of these kinds of accesses is described in more detail in subsections later in this chapter.

Two Issues in Using Table-Driven Methods

The second issue you have to address if you're using a table-driven method is what you should store in the table. In some cases, the result of a table lookup is data. If that's the case, you can store the data in the table. In other cases, the result of a table lookup is an action. In such a case, you can store a code that describes the action or, in some languages, you can store a reference to the routine that implements the action. In either of these cases, tables become more complicated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223