I was looking at the null object pattern and i am wondering if it worths implementing it or use “if” checks for nulls in my code intsead. As i looked at the implementations it seems hard to keep objects well synchronized with their null implementations. By making changes to the main object we have to check that the null object behaves as expected and its easy to make implementation errors. Isn’t it ?
EDIT
Avoid checking for null: http://www.invisible-city.com/sharon/2009/03/null-object-pattern-when-slacker-is.html or http://journalofasoftwaredev.wordpress.com/2008/08/19/null-object-pattern-by-example/
From my experience I don’t feel it is hard to keep a Null-Object in sync. If you do have this problem, your object may have too many responsibilities as it is subject to a lot of change.
I tend to use this pattern if I can actually define such an object in terms of letting a default behaviour flow well. E.g. A Null Object for a Query Manipulation is one that leaves the original query untouched. A Null Extension to some other object is one that does nothing.
I find it at times a pretty clean pattern to avoid checking with ifs for optional behaviour that may or may not exist at a certain state of my app.