Null Object pattern

Discussions about design and architecture principles, including native JADE systems and JADE interoperating with other technologies
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Null Object pattern

Postby ConvertFromOldNGs » 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 ----------------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Null Object pattern

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:24 am

by Craig Shearer >> Sun, 13 May 2001 21:45:56 GMT

Hi Darrell

I can't say I've tried this, but my initial reaction is that it might lead to lazy coding. In my experience with JADE designs there are three situations when dealing with references:

1. Sometimes you have situations where a reference is validly null
2. Other times where it being null would indicate an error (integrity, design, etc.).
3. In other cases, a reference cannot be null simply because you have configured automatic inverse relationships in such a way (or written the code in such a way) that there must always be a reference.

I've seen developers write code which unnecessarily checks a reference for null (in the case where the reference cannot be null, or where being null would indicate an error). Doing this either is inefficient (if the reference can never be null) or hides errors (when a reference was invalidly null). Whatever, I usually expect to see some comment about why the developer is checking for null.

So, I'd be concerned about the applicabilty of this pattern.

I'm not sure how this would work in JADE anyway. Wouldn't you need to have a specifc "null" instance of each class so that the "null" object had the right interface. And then, would it be a singleton, if there are other non-null instances of the class also.

Also, and I know this is poor practise, but developers have the tendency to write stuff like:

if myObject.myOtherObject.myRefernce.rate > 0 then

where there is a string of references to get to the desired "thing". Where would you stop, or would there be the possibility of each of these references along the way having a "null" value.

On a lighter note, I recall back in a beta (or perhaps even alpha) version of JADE, somebody managed to delete the null object in JADE (somehow, don't ask me how!), so perhaps JADE already implements such a scheme.

Craig. (in Auckland, where it's wet, and the worst possible day to experience battery failure in a car!)

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Null Object pattern

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:24 am

by Iain S. Kerr >> Sun, 13 May 2001 21:54:19 GMT

Darrel
I have tried this with Security aspects of my system.
When a user logs in the system works out whether the person has access rights to a particular class of objects. for example BankAccount class. When the user then says they want access to a Bank Account instances the system checks whether the user is allowed and if so returns the collection or instance as the case may be. If they are not allowed then the system returns the Singleton with a name set to "Access Denied".

It is a reasonable technique and helps prevent the continual checking for null as you have stated. However you cannot eliminate it completely as very often a particular chain may have a null in the middle of it and therefore you will get an error at run time and you will have to hanlde it via your internal exception system

Iain

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Null Object pattern

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:24 am

by Wilfred Verkley >> Tue, 19 Jun 2001 23:35:43 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.


Im finding that were using it a lot in our current application.

Our application stores a lot of "Widget" objects that our users need to reference in their data. But we are finding there are a lot of Widget's out in the real world that we dont know about. So we usually add a "OTHER" Widget into our lookup collection. This object is sometimes a subclass of a "real" widget class that overrides certain methods to implement or return more generic behaviour/data. Often much tidier and more object-orientated then holding a null value instead and doing if-null tests everywhere it used.

I think the naming of this pattern is misleading however. A better name for it would be something like "generic object pattern".

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Null Object pattern

Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:24 am

by trinav@bigpond.com >> Thu, 5 May 2005 10:37:23 GMT

Darrell, I don't know what the hell you're talking about, but I would like to know if you are Professor D A Duniam of NW Tas? If so howdy doody - long time no see!


Return to “Design and Architecture”

Who is online

Users browsing this forum: No registered users and 12 guests