Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
June 6, 2001-- At Tuesday's "Effective Programming with Java Technology" conference session (TS-2425), Joshua Bloch, senior staff engineer from Sun Microsystems, presented some powerful, but not well known, idioms that should improve the effectiveness and quality of your Java programming. These idioms were taken directly from Bloch's new book, Effective Java Programming Language Guide (Addison-Wesley, 2001).
Bloch's session covered:
When you want to create an instance of a class (an object), you normally do so by calling new on the class's constructor. As an alternative to new, the Static Factory pattern provides a static method that returns an instance.
Bloch took the following example of a static factory straight from Java:
public static Boolean valueOf(boolean b) {
return (b ? Boolean.TRUE : Boolean.False);
}This method appears in the java.lang.Boolean class.
Bloch noted a number of advantages to using static factories instead of constructors to create objects.
First, static factories have names while constructors do not. Constructors must have the same name as the class and may vary only in their type and number of arguments. Static factories, on the other hand, can have different names, as well as any type and number of arguments. Bloch used an example of a complex number to drive this point home:
public static Complex valueOf(float re, float im) //... public static Complex valueOfPolar(float r, float theta) // ...
In the code above, the static factory is much more expressive. Furthermore, the equivalent constructors would have conflicted with one another, since they would have the same number and argument types.
Second, Bloch explained that a static factory is not required to create a new object each time it is called. This means that
you can reuse immutable objects and cache frequently used values (such as Boolean's TRUE and FALSE). Such reuse can lead to improved performance. With static factories, you can also control and regulate
which objects exist at any given time. You can't do that with new.
Finally, Bloch pointed out that static factories can return objects of any subtype. Creating an object using a constructor will only create objects of a specific type. Using a static factory in this way creates a nice separation between interface and implementation. Beyond supporting polymorphism, the ability to hide an object's subtype is powerful. The class of the object that is returned need not be public, leading to compact, easier-to-understand APIs. The result: it's easier to make changes to the API from release to release, and even during runtime.
As useful as it is, the static factory does have some disadvantages.
Bloch explained that a static factory cannot be subclassed. Normally, a static factory will make its constructor private so that other objects cannot instantiate it directly. However, Bloch maintained, this is a blessing in disguise, since inheritance is not always the best form of reuse anyway.