Item 1: Consider static factory methods instead of constructors

Before starting the blog entry, lemme just make a quick comment on “Effective Java”, the book. It helps you think better. I am finding it awesome. Also, I would love to to express how grateful I am to the person who introduced me to it.

So, static factory methods replacing the good old constructors, does that really make sense? Well, yes it does.

Example 1
Remember Singleton design pattern? 😉

/**
 * Singleton implementation
 * 
 * @author lekhak.tumblr.com
 */
public class Calculator {
	// Single instance of Calculator, lazy initialized
	private static Calculator instance = null;

	// private constructor
	private Calculator() {
	}

	// Returns an instance of the calculator
	public static Calculator getInstance() {
		return (null == instance) ? instance = new Calculator() : instance;
	}

	// Dummy public method. Such methods can be only called using the reference
	public int add(int x, int y) {
		return x + y;
	}
}

Example 2
Another example of the static factory method can be the valueOf method in Boolean class. The method returns the object of Boolean, but is not a constructor for Boolean class.

// valueOf returns an object
Boolean sampleBoolean = Boolean.valueOf("true");

Item 1 of effective java is the static factory methods. These methods we are talking about is not to be confused with the factory method design pattern.

Well the advantages of static factory method stated by the book are-

  1. One advantage of static factory methods is that, unlike constructors, they have names.
    This is definitely a plus for static factory method, they increase readability of the method, as they can be given a proper name.
  2. A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.
    A constructor is bound to create a new object once it is called. But as for static factory method, this is not the case. It may return a modified or pre-existing object.
  3. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
  4. A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.
    When invoking a constructor, one must specify the parameter types, even when they are quite obvious. When invoking a static factory method, one may avoid the use of parameter types.
     
    Map<String, List<String>> m =
        new HashMap<String, List<String>>();
    

    Can be replaced with ..

    public static  HashMap<String, List<String>> newInstance() {
        return new HashMap();
    }
    
    Map<String, List<String>> m = HashMap.newInstance();

The main disadvantages of static factory methods are –

  1. Providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
    Whenever a constructor of subclass is called, the constructor of the parent class is also called. In the absence of protected or public constructors, sub classing or extending a class will make no sense.
  2. A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.
    Constructors can be readily distinguished as they have the same name as that of the class itself. 

Feel free to google “static factory methods”. Or I’ll be glad to take up questions in case you have one. Just open this post on http://lekhak.tumblr.com, and add comments.

Advertisements

About Gaurav

I'm Gaurav; friends call me Teddy, n i'm shivi fr my family!! A java enthusiast who likes experimenting with what he knows. View all posts by Gaurav

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: