Reusable Parameters Object Reuse

- 83 - } private static Hashtable hvectors = new Hashtable ; This method is equivalent to the getVector method, but works prior to JDK 1.2 as well as after. public static Vector getVectorPriorToJDK12 { Lazily initialized version. Get the thread local object Vector v = Vector hvectors.getThread.currentThread ; if v == null { First time. So create a vector and set the thread local v = new Vector ; hvectors.putThread.currentThread , v; } return v; } }

4.2.3 Reusable Parameters

Reuse also applies when a constant object is returned for information. For example, the preferredSize of a customized widget returns a Dimension object that is normally one particular dimension. But to ensure that the stored unchanging Dimension value does not get altered, you need to return a copy of the stored Dimension . Otherwise, the calling method accesses the original Dimension object and can change the Dimension values, thus affecting the original Dimension object itself. Java provides a final modifier to fields that allows you to provide fixed values for the Dimension fields. Unfortunately, you cannot redefine an already existing class, so Dimension cannot be redefined to have final fields. The best solution in this case is that a separate class, FixedDimension , be defined with final fields this cannot be a subclass of Dimension , as the fields cant be redefined in the subclass. This extra class allows methods to return the same FixedDimension object if applicable, or a new FixedDimension is returned as happens with Dimension if the method requires different values to be returned for different states. Of course, it is too late now for java.awt to be changed in this way, but the principle remains. Note that making a field final does not make an object unchangeable. It only disallows changes to the field: public class FixedDimension { final int height; final int width; ... } Both the following fields are defined as final public static final Dimension dim = new Dimension3,4; public static final FixedDimension fixedDim = new FixedDimension3,4; dim.width = 5; reassignment allowed dim = new Dimension3,5;reassignment disallowed fixedDim.width = 5; reassignment disallowed fixedDim = new FixedDimension3,5; reassignment disallowed An alternative to defining preferredSize to return a fixed object is to provide a method that accepts an object whose values will be set, e.g., preferredSizeDimension . The caller can then - 84 - pass in a Dimension object, which would have its values filled in by the preferredSizeDimension method. The calling method can then access the values in the Dimension object. This same Dimension object can be reused for multiple components. This design pattern is beginning to be used extensively within the JDK. Many methods developed with JDK 1.2 and onward accept a parameter that is filled in, rather than returning a copy of the master value of some object. If necessary, backward compatibility can be retained by adding this method as extra, rather than replacing an existing method: public static final Dimension someSize = new Dimension10,5; original definition returns a new Dimension. public Dimension someSize { Dimension dim = new Dimension0,0; someSizedim; return dim; } New method which fills in the Dimension details in a passed parameter. public void someSizeDimension dim { dim.width = someSize.width; dim.width = someSize.height; }

4.2.4 Canonicalizing Objects