Sunday, September 23, 2007

Constructing Java Objects with Thread-safety, Reusability and Encapsulation in Mind

After a long hiatus (during which I gained a new-found respect for the prolific bloggers who post daily), I am finally back with my first article titled Constructing Java Objects with Thread-safety, Reusability and Encapsulation in Mind. Please provide your feedback in comments. Hope you find the article useful.
PS: I could not post the article directly in the blog since it messed up the code formatting. Hence I posted it on my web site.


Clay said...

Menon - very well written article!

While this is a very basic topic, it is covered in a refreshing way and it is always good to reinforce good habits (and stop bad ones) by giving these issues conscious thought.

When I saw the title I was hoping that the article might go into some more advanced topics such as using double checked locking to instantiate static variables in a thread safe fashion (coincidentally Ivan, Vinod and myself were having a debate about this topic just this afternoon).

Also, it might be nice to continue the article with an example of a thread safe object cache, where already instantiated Employee instances were looked up based on the unique combination of name and age.

A (very minor and nitpicky and subjective) point - I have a pet peeve about classes named "Util". Over time, they often end up cluttered with a grab-bag of many unrelated methods. If intended for generic parameter validation, maybe this class would better be called GenericParameterValidator.

Menon said...

Thanx Clay. The idea of the article was to combine some of the well known practices into one context -object construction. That is why I did not delve into other topics such as object cache etc - they are related to object construction but not in a direct way. Regarding the Util class naming - I usually give a name such as DbUtil etc., in this case I gave the name Util since I wanted to indicate that it is a grab bag class of many potentially un-related methods :) Thanx for the nit pick anyways, I will keep it in mind as far as naming connventions go.