Chapter 12 . Library Classes


Chapter 12. Library Classes

Exercise 44. Layers

A.

graphics/ap01inf01.gif

B. In the first case, code depends directly on the library. In the second case, it depends directly on the layer and only indirectly on the library.

C.

  • Conceptual Integrity : It depends ”a good layer interface can improve the way we think about things.

  • Portability : Better; changes will be concentrated in the layer.

  • Performance : It can go either way. There's a small cost to going through the layer, but the layer may be able to cache data or otherwise speed up performance.

  • Testing : It may be easier to test in the layer, especially if the layer's interface is narrower. It may make it easier to swap in a test implementation as well.

D. Java doesn't have language mechanisms to enforce it. You might have external mechanisms (e.g., a tool that checks references to the layered packages).

Exercise 45. Trees

A. A foreign method seems easier. Since we already have to modify our node to implement the interface, we can put it there. A local extension might be a new subinterface of MutableTreeNode; this would seem better only if the tree clients could use the userObject without caring about our node type.

Exercise 46. String

A. Declaring a class final prevents creation of a subclass. This makes it impossible to use Introduce Local Extension .

B. There may be other reasons, but one reason the String class is final has to do with security. In Java, strings are immutable ”once set, they cannot be changed. Some security checks rely on this fact. A subclass might subvert immutability . (There may be a performance benefit as well; substring operations can point to part of a string known not to change.)

Exercise 47. Filter

B. Implements Enumerator.

C. Takes an enumerator (the one to be filtered).

D. Defines the required methods ( hasMoreElements() and nextElement() ), and a new method isValid() for its subclasses to override.

Exercise 48. Diagrams

graphics/ap01inf02.gif

Exercise 49. A Missing Function

It's probably easiest to introduce a foreign method on one of your classes.




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

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