aspose file tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes NullObject Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "NullObject" Watch "NullObject" New topic
Author

NullObject

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

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: 6913
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.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullObject
 
Similar Threads
Need help to write this in a better manner(Refactoring)
Null objects
should a method return null anytime?
Null Object Pattern and state pattern question
Refactoring Databases: Schema Evolution and Maintaining Data