Exercise 44 Layers. (Challenging).

One way to deal with libraries is to put them beneath a layer. This lets you isolate the bulk of your code from direct dependency on other libraries. Consider these two alternatives:


  1. Redraw this as a UML package diagram showing dependencies.

  2. Explain how the bulk of your code does or does not depend on the library code in each of these situations.

  3. What effects does this layering have in terms of:

    • Conceptual integrity?

    • Portability?

    • Performance?

    • Testing?

  4. What mechanisms do you have available to enforce the layering (that is, what stops someone from turning the second approach into the first one)?

See Appendix A for solutions.

Exercise 45 Trees.

The Swing tree library (see javax.swing.tree.*) has an interesting flaw. This library has the idea of a DefaultTreeModel (class) built out of MutableTreeNodes (interface) objects. Although there is a DefaultMutableTreeNode class, you might not want to use it if you already have node-like objects. (You might prefer to implement the MutableTreeNode interface rather than copy your information to a DefaultMutableTreeNode.)


The flaw in the library is a gap in the MutableTreeNode interface: It supports a method setUserObject() , but no method getUserObject() .

Suppose you'd like to have this method.

  1. Does Introduce Foreign Method or Introduce Local Extension seem more appropriate? Why?

See Appendix A for solutions.

Exercise 46 String.
  1. The String class in Java is declared final . What effect does this have on Introduce Local Extension ?

  2. Why would the creators of Java make String final?

See Appendix A for solutions.

Exercise 47 Filter. (Challenging).

The Collections class in java.util defines a number of utility methods , including one to produce an enumeration for a collection. This enumerator gives a list of all elements, but it doesn't provide a way to filter them.

  1. Write a class to wrap Enumerator, extending it with the ability to return objects that meet some criteria. (Include tests.)

  2. What parent class or interface does your class have (if any)?

  3. What arguments does the constructor take (if any)?

  4. What methods does it define?

See Appendix A for solutions.

Exercise 48 Diagrams. (Challenging).

Draw class or other UML diagrams to help explain and show the difference between Introduce Foreign Method, Introduce Local Extension , layering, and wrapping.

See Appendix A for solutions.

Exercise 49 A Missing Function.

Consider the Zumbacker Z function, at the core of your application. (In fact, it's such a commonly used function in your domain that you're a little surprised it's not in the Java math libraries already.) It's defined:

Z(x) = abs[ cos(x) + sin(x) “ exp(x) ]

How could you handle the problem of java.lang.Math being an incomplete library?

See Appendix A for solutions.

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: