I have been writing posts about effective C++ from the book by Scott Meyers but there is a similar book by Joshua Bloch that addresses similar tricks and tips for Java. Many Java designers believe that their language of choice does not require the attention to detail that languages like C++ does but that is actually not the case. I can remember when I discovered my first Java memory leak because it was such a traumatic event.
What follows is my interpretation of the 78 items that Joshua believes are key to writting effective Java programs.
1: Consider static factory methods instead of constructors: The first thing to point out here is that a static factory method is not the same as a factory pattern. A static factory method is simply a method that returns an instance of the class. The first advantage of this approach is that static factory methods have a name where constructors in Java do not. If you choose the name carefully it can make code easier to read.
Advantage 1: a class can have only one constructor with a unique signature. You can have as many factory methods as you need and they can all have the same number and type of variables. You would name them differently to clearly indicate what each one does.
Advantage2: Factory methods are not required to return and object every time. This provides a mechanism for immutable classes to use or reuse pre-constructed instances. This is particularly helpful when creating large numbers of objects. You can have a pool of message objects for example, that get used and placed back in the pool.
Advantage 3: Factory methods can return an object of any subtype. There are several frameworks in Java where there are large numbers of interfaces. The Java collection framework for example, has thirty-two different public classes, one for each convenience implementation. By using a factory method, the programmer refers to an object by its interface and the proper convenience implementation is returned.
Advantage 4: Factory methods do not require the programmer to specify unique type signatures which can lead to verbose and somewhat redundant code. You could argue this one either way in my opinion. Yes you end up typing the parameters potentially twice in rapid succession, but I don’t have a problem with that and I believe it clarifies what the programmer intended.
Disadvantage 1: By providing only static factory methods, classes without public or protected constructors cannot be sub-classed. Depending on what you are attempting to do this could be bad or good. If the class in question is a singleton for example, this guards against multiple instances.
Disadvantage 2: Static factory methods are not distinguishable from other methods. Constructors stand out in the code because they look different, where static factory methods do not. Once again this can easily be addressed through the use of a coding standard. If the designers agree to always place static factory methods at the top of the class, they can be easily found.
This first topic was quite lengthy so I think I will stop there. The object of these daily posts is to give the reader something to think about each day. I have not used static factory methods in much of my code, but after reading this I can see the advantages.