This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes what are the advantages of using Null object pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "what are the advantages of using Null object pattern?" Watch "what are the advantages of using Null object pattern?" New topic
Author

what are the advantages of using Null object pattern?

shreyas Kulkarni
Greenhorn

Joined: May 06, 2011
Posts: 4
Can anyone please tell me what are the advantages of using Null object pattern? I tried to search too. I used it in my doubly linked list as first and last node(head and tail) but I am not able to figure out its use.
Thanks
Andi Eska
Greenhorn

Joined: Jun 20, 2003
Posts: 14

shreyas Kulkarni wrote:Can anyone please tell me what are the advantages of using Null object pattern? I tried to search too. I used it in my doubly linked list as first and last node(head and tail) but I am not able to figure out its use.
Thanks

Using the Null Object Pattern you can avoid null checks and NullPointerExceptions

The Null Object pattern is described in
Robert C. Martins book Agile Software Development, Principles, Patterns, and Practices

Here is the example - let's assume that you have an application that check whether the user is authenticated:

and the code that returns the reference to the User object looks like this:



This way the code that checks whether our user is authenticated should look like the following snippet:



Checking whether the object is null is not only a boilerplate code but it can also give you a lot of bugs e.g. if you forget to check whether the object is null.

And here the Null Object can help you:



plus:


plus cleaner client code:



andi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what are the advantages of using Null object pattern?
 
Similar Threads
DAO Pattern
DAO to Entity Bean
Benefits of using REST style
Proxy pattern and advantages/disadvantages
what is the use of threadgroup?Advantages of it?