This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "NullObject" Watch "NullObject" New topic
Author

NullObject

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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 ?
Thanks
Pho


Regards,

Pho
Frank Carver
Sheriff

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 frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NullObject
 
Similar Threads
Refactoring Databases: Schema Evolution and Maintaining Data
Need help to write this in a better manner(Refactoring)
Null Object Pattern and state pattern question
Null objects
should a method return null anytime?