Sunday, April 10, 2011

Creating and Destroying Objects - Part 1


Consider static factory methods instead of constructors
Advantages:
1.      You can choose a meaningful name for the factory method which is not possible with constructors.
2.      Static factory methods can choose when to create an object. They can reuse the existing object instead of creating a new one by caching the instances as and when being constructed (Classes doing this are called instance-controlled).
3.      Based on the arguments, the static method factory can decide to return a different sub-type of the promised return type which need not be even public.
4.      They reduce verbosity of creating parameterized types (Shown below).
Disadvantages:
1.      If you provide only static factory method and make constructors private, you can't subclass it.
2.      The API documentation will have a list of static factory methods along with other static methods, and there is no clear way to differentiate them.
Conventions:
1.      Static factory methods are sometimes provided in a dedicated class. If the type they are going to return is called Type (E.g. Car), the class containing the static factory methods is called Types (E.g. Cars).
2.      Static factory methods usually have the following names. ValueOf, getInstance, newInstance, getType, newType.
Code where verbosity is reduced:
Map<String, List<String>> m =  new HashMap<String, List<String>>();

Can be expressed as:
public static <K, V> HashMap<K, V> newInstance() {
    return new HashMap<K, V>();
}
Map<String, List<String>> m = HashMap.newInstance();


Consider a builder when faced with many constructor parameters.
1.      Telescoping constructors (providing multiple constructors) wont scale when there are too many parameters to be added. And it will make the life of client very complicated.
2.      JavaBeans pattern (providing setters for all the fields) might leave object in an inconsistent state when client doesnt set all values well. You may want to freeze the object till it is being built correctly and then unfreeze it. However this solution is not being used in general.
3.      Using Builder Pattern is a better choise as it simulates a named optional parameter and the build() method can ensure that all required values are set properly.
4.      Disadvantage of using builder is that the client side code becomes more verbose.

Enforce singleton property with a private constructor or an enum type.

Enforce noninstantiability with a private constructor
1.      There are cases where you want a class not to have any instance. The class might just be a grouping of a collection of static utility methods, or static factory methods.
2.      You can make the class abstract and thereby prevent direct instance being created for that class. However this can be broken easily by extending the abstract class and creating objects of the inherited class.
3.      Make the constructor private and then throw an exception from within so that no one can create (even from inside the class) an instance of this class.
As a side effect, making the constructor private prevents the class from being extended.

I have taken these points from the book "Effective Java" which I consider as a MUST READ book for every JAVA developer.

No comments:

Post a Comment