posted 18 years ago
You would have seen lot of code like the following piece
If there are multiple calls to the getProduct() method, the null check has to be done in all the places. The Null Object Pattern comes for the rescue here.
The idea is to make the getProduct() method return a dummy instance (of Product type), which provide some default behaviour.
So, the calling client will get an instance of NullProduct instead of null. No need to do any null check.
A better way to implement the pattern is to declare the NullProduct class inside the Product interface, and use the same instance everywhere.
Hope that helps.
Mani
Quaerendo Invenietis