This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes NullObject Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "NullObject" Watch "NullObject" New topic


Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 776

Here's the intent for the NullObject:

Encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior.

In the situation where I have a JDBC Connection reference which I check for null; and if it's null, I will attempt to create a NEW connection - does that qualify for the use of a NullObject pattern ?


Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Only if there are other places in the code where the connection is used, even if it may not have been opened.
The typical use of this pattern is when you have code which would like to use the result of an operation, even if it failed. eg:
Here's an example using a traditional null, return value

Now here's the same code using the NullObject pattern:

Note that by guaranteeing that "listNames" will always return a valid array, even if it is an empty one, the code which uses it is made much simpler and more readable.
I hope that makes sense.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
I agree. Here's the link:
subject: NullObject
It's not a secret anymore!