Null Object Pattern: real world examples
Recently I learned about the Null Object Pattern and got intrigued. I searched the Internet and there is a fair amount of information available. I think it can be a very beneficial pattern to most applications. Essentially, it prevents the application from having to check for null values all the time. In fact, LINQ did a good job of almost never returning a null collection, but rather an empty collection, to the caller.
My question is does anyone have any real-world experiences or stories where it clearly demonstrates how it was used and benefitted the developers? I guess what I am wondering: is there a best-practice approach to implementing and applying this pattern?
Thanks in advance!
Walt Lynton, Mar 29, 2011
I was struk by the same bug and my search led to this example :
hope this helps.
Ranjith Rajan, May 21, 2011
It is of immense use in huge data centric application as well. For e.g. many financial/banking industries there are lots of calculations that happens in bulk mode wherein you read lots of data and some processing happens on that. It may be millions in numbers. This pattern comes in very handy (as I have faced situations like this many time).
Create a null object for the said entity and process everything in bulk. The null object logs the state and the processing is continued without being interrruped to unwanted null exceptions (note if they happen they are automatically logged :))...
Though this can be done without using NULL Object pattern, but you can imagine the no. of not null check that you need to make for this....
Rajesh Pillai, Mar 30, 2011
I guess there are many examples, but I particularily find it useful when working with the Command Pattern.
Say you build a Server that takes some input, given a specific input a command is executed. When faulty data is sent to the server I create a Null Object Command instead of null which does nothing when executed. This keeps me from checking for null.
Just my 2c :)
Robert Blixt, Mar 29, 2011