Chapter 8 . Data


Chapter 8. Data

EXERCISE 24. Alternative Representations

Money (based on U.S. currency, where 100 cents = 1 dollar, and a cent [a penny] is the smallest coin):

  • Integer count of cents.

  • A pair of integers managed as a long long .

  • Use a decimal type.

  • You may have to track fractions of pennies. (Some money is managed in terms of 1/10 cent.)

  • String.

Position (in a list):

  • Integer.

  • If there's only one position of interest, you might manage the list (as seen from outside) via two lists, one containing what comes before the position and the second containing what comes after the position.

  • The item at that position.

Range:

  • First and last index

  • First index and length

Social Security Number (government identification number: "123-45-6789")

  • String

  • Integer

  • Three integers

Telephone number:

  • String

  • Integer

  • Two numbers : area code and local number

  • Three numbers : area code, exchange, and last four digits

This only considers U.S. phone numbers; it will be more complicated if you add international phone number support. You also may have to support extensions.

Street Address ("123 E. Main Street"):

  • String

  • Multiple fields

  • Physical coordinates

  • Standardized address (standard abbreviations)

  • Index in a standard list of addresses

ZIP (postal) code:

  • String

  • Integer

  • Two integers (U.S. postal codes now use ZIP + 4 or 12345-6789)

  • Index in a standard list of codes

EXERCISE 25. A Counterargument

It depends on what's happening between the screen and the database. If it's truly a form-filling application, to get this field from the screen into that field on the database, we might not use an object-oriented approach. But as more functions are added that concern ZIP codes (validation, computing shipping distances, mapping routes, etc.), we'll expect more benefit from the object-oriented approach.

EXERCISE 26. Iterator

When we use the Iterator, the code relies less on the "for" loop approach, so there will be less use of integers as position counters.

EXERCISE 27. Editor
 assertEquals(___, editor.fetch(1)); 

A. Given the interface provided, what string would you expect to use in place of the ___?

 "a" 

B. Based on the variable name (firstParendPosition) , what string might you like to use instead? Of what use would this be?

 "(" 

It is sometimes useful to have positions that remember where they are, even if text is inserted in front of them. For example, a programming editor might track the position of each method declaration.

C. The crux of the problem is the use of int as a position index. Suggest an alternative approach.

Instead of handing out dead integers, hand out Position objects, but let the editor own them. When text changes, the editor updates the Positions. The holders of the objects aren't aware of that; they just know that they can get one or hand it back to move to a prior position.

D. Relate your solution to the Memento design pattern.

Memento uses an opaque object; in this case, the editor may know what's inside, but clients definitely don't. The client can't manipulate the Memento pattern directly, but must hand it back to the main object to use it.


EXERCISE 28. Library Classes
  • All have public data members .

  • All are subclasses of the class Object.

  • Most are very stable.

  • Most have a well- understood meaning outside of their use in Java (with the exception of GridBagConstraints).

  • There may be a lot of them.

EXERCISE 29. Color and Date

A. The Color constructor shows three different representations: an RGB (Red/Green/Blue) triple of integers, a single integer holding all the values, or an RGB triple of floats. The color value could be an HSB (Hue/Saturation/Brightness) triple as well.

Date could be stored as a set of values (year, month, day, etc.), as an integer count (seconds or microseconds since some event), or it could even be stored as text.

B. Because clients have no direct access to the fields, they can't change an instance behind that object's back (without going through its methods ).

EXERCISE 30. Proper Names

A. Client 1 produces a string in first-name-first format; clients 2, 3, and 4 produce a last-name-first string. Put methods on Person for these two variants. You may then be able to Encapsulate Field , making the fields private.

B. It will be easier to handle these changes once the duplication is consolidated.




Refactoring Workbook
Refactoring Workbook
ISBN: 0321109295
EAN: 2147483647
Year: 2003
Pages: 146

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net