Null Object pattern
Posted: Fri Aug 07, 2009 11:24 am
by Darrell Duniam >> Sun, 13 May 2001 4:09:46 GMT
Hi,
Has anyone out there tried implementing the Null Object pattern ? The Null Object pattern is an alternative to using a null value to indicate the absence of an object reference. Using null to indicate the absence of an object reference requires a test for null before each call to the other object's methods. Instead of using null, the Null Object pattern uses a reference to a Singleton object that doesn't do anything, yet has the same interface. Each kind of Null Object is a singleton because nothing on that object ever changes.
An example given where I've read about this (Refactoring [Fowler]) is asking an object for its Person, then asking the Person for its "rate". Where the reference to Person returned a Person kind of Null Object, the returned rate will simply be zero.
This strikes me as very attractive, yet I can see pitfalls, such as debugging. Has anyone tried this pattern, and how would you rate it ?
regards,
darrell. ---------------------------------------------------------------------------- Darrell Duniam, JADE eCommerce Development
dduniam@jade.co.nz
Ph: 03-358-0102 Fax: 03-358-4882
Internet: http://www.jade.co.nz
Intranet: http://aorakinet/jed/default.htm ----------------------------------------------------------------------------
Hi,
Has anyone out there tried implementing the Null Object pattern ? The Null Object pattern is an alternative to using a null value to indicate the absence of an object reference. Using null to indicate the absence of an object reference requires a test for null before each call to the other object's methods. Instead of using null, the Null Object pattern uses a reference to a Singleton object that doesn't do anything, yet has the same interface. Each kind of Null Object is a singleton because nothing on that object ever changes.
An example given where I've read about this (Refactoring [Fowler]) is asking an object for its Person, then asking the Person for its "rate". Where the reference to Person returned a Person kind of Null Object, the returned rate will simply be zero.
This strikes me as very attractive, yet I can see pitfalls, such as debugging. Has anyone tried this pattern, and how would you rate it ?
regards,
darrell. ---------------------------------------------------------------------------- Darrell Duniam, JADE eCommerce Development
dduniam@jade.co.nz
Ph: 03-358-0102 Fax: 03-358-4882
Internet: http://www.jade.co.nz
Intranet: http://aorakinet/jed/default.htm ----------------------------------------------------------------------------