Page 1 of 1
Null object pattern
Posted: Sun Feb 17, 2008 1:04 pm
by piccoloprincipe
I have implemented the null object pattern for some of my classes, but it requires one interface extraction and one class (for the null object) for every class in my environment.
Is there a faster way to construct null objects?
Re: Null object pattern
Posted: Sun Feb 17, 2008 1:59 pm
by Christopher
You could create an implementation of the Null Object pattern in PHP by just doing:
Code: Select all
class NullObject {
public function __call($name, $params) {}
}
That is actually a pretty nice idea. Take that Ruby, Python and Java!!!!

Re: Null object pattern
Posted: Sun Feb 17, 2008 5:07 pm
by piccoloprincipe
That's useful
But I was thinking that's a reason for NullObject having to implement the specific interface of the relative class: consistency of the methods. Think of a method hasNotPermission() that have to always return TRUE in case of the null object: I have come to the conclusion that null object should have a specific behavior for every class that it's linked to, and clean code have to specify more and more null objects.
Re: Null object pattern
Posted: Sun Feb 17, 2008 9:10 pm
by Christopher
How about:
Code: Select all
class NullObject {
protected $methods = array();
public function __call($name, $params) {
return isset($this->methods[$name]) ? $this->methods[$name] : null;
}
}
class MyClass extends NullObject {
protected $methods = array('hasNotPermission' => true);
}
Re: Null object pattern
Posted: Sun Feb 17, 2008 11:08 pm
by Chris Corbyn
Scary
EDIT | Sucks for type-hinting though.
Is there a valid place this would be used? I could see it being potentially useful in testing but using it in production would scare the hell outta me

Re: Null object pattern
Posted: Sun Feb 17, 2008 11:32 pm
by Christopher
I get the impression that you would provide one of these object at initialization so the object using it would not need to do null pointer checks. It would not error and would return "nothing happened" type results. Then (I guess

) you would provide a fully functional object as a replacement at some point. Maybe for things like callback architectures?
Re: Null object pattern
Posted: Mon Feb 18, 2008 5:02 am
by sike
i use the concept of "null" objects quite alot. it helps making my code easier to read and shortens it quite a bit.
a simple example is iterating over a collection of items :
i always return a iterator regardles of the fact that there is something to iterate. if there is nothing to iterate a NullIterator is returned which a) signals that there is nothing to iterate and b) behaves exactly like a normal iterator. that pays off in terms of not having to check if the returned iterator is valid.
Code: Select all
var it:Iterator = foo.getIterator();
if (it)
{
while(it.hasNext())
{
}
}
vs
Code: Select all
var it:Iterator = foo.getIterator();
while(it.hasNext())
{
}
there are many places where this concept is useful but fowler is better at explaining this kind of things :
http://books.google.com/books?id=1MsETF ... LtIALWUNcw
cheers
Chris
Re: Null object pattern
Posted: Mon Feb 18, 2008 5:07 am
by Benjamin
My question would be: Is the time spent creating and maintaining this extra code less then the time spent adding a few characters to the while loop? I just don't see it. Seems bloatish to me.
vs
Re: Null object pattern
Posted: Mon Feb 18, 2008 5:14 am
by sike
thats the problem with simple examples (;
my main use for nullobjects is reducing tedious error checking on returned values. granted "it && .." does the job on that particular example well enough but thinking of more complex things it get smore and more useful (at least for me)
chris
Re: Null object pattern
Posted: Mon Feb 18, 2008 5:20 am
by Benjamin
sike wrote:thats the problem with simple examples (;
my main use for nullobjects is reducing tedious error checking on returned values. granted "it && .." does the job on that particular example well enough but thinking of more complex things it get smore and more useful (at least for me)
chris
I know what you mean, I've got some pretty wacked out code as well. But in the end it does some incredible things. The closest thing I have ever used to this is an empty function that does nothing except return true. I can't remember offhand what I used that for though.
Re: Null object pattern
Posted: Mon Feb 25, 2008 3:21 pm
by Ollie Saunders
That is actually a pretty nice idea. Take that Ruby, Python and Java!!!!
*Cough* I think you'll find Ruby does it just as easily...
Code: Select all
class NilResponder
def method_missing(name, args)
end
end
...possibly better because you would need to define __get() and __set() in PHP to match what method_missing does.
This is a
documented refactor. I've used it before in PHP but it's a bit of a hassle because, as mentioned, of type hinting. It works much better in Ruby where you don't type hint.
Re: Null object pattern
Posted: Mon Feb 25, 2008 7:12 pm
by Chris Corbyn
__get() and __set() handle property access. Does method_missing() do a combination of faux method invokation and property access?

Re: Null object pattern
Posted: Mon Feb 25, 2008 8:02 pm
by Ollie Saunders
Does method_missing() do a combination of faux method invokation and property access?
Considering you already use methods to get access to data in PHP there's really nothing fauxy about it using methods to access data. As such Ruby doesn't have public properties at all. Everything is a method. But methods are not named beginning with 'set' and 'get' so the distinction between data and behaviour is based entirely on the name of the method which is perfectly sufficient.