9
I asked that question What is the purpose of Emptyiterator? at SOEN, because I found no answers here.
Then I was told in a response from the SOEN of a pattern called Null Object
.
What is the purpose of this standard and how it works?
9
I asked that question What is the purpose of Emptyiterator? at SOEN, because I found no answers here.
Then I was told in a response from the SOEN of a pattern called Null Object
.
What is the purpose of this standard and how it works?
6
It is an object created to simulate another object with the same contracts, but without any functionality. No state and no real behavior. The two types are compatible with each other since they meet the same contract, implement the same interfaces and possibly extend the same type.
It is a way to avoid using the null state of objects that most languages use. When there really is a semantic need, a situation where an object of a type is null, you use this object that meets the same contracts but does nothing.
This form tends to be "more object-oriented" by providing a better abstraction for an unknown value situation, avoids side effects and facilitates coding that does not need to treat specific cases as an exception to the rule.
There is a bit of controversy about its use since it can also end up hiding some programming errors that would be caught if it used a null value, which would be a completely different and incompatible type.
The criticism occurs because despite being fulfilling the contract, does something unexpected. And it turns out that there are people who criticize other resources like operator overload because someone can make a subtraction on the addition operator. In this case it is certain that the method does something different than expected. Finding logic error is much harder than finding programming error. And this pattern induces logic error. So it is rare to see it being used. And even rarer compendium of design patterns citing it.
Improving the OS response example:
interface IAnimal {
public function makeSound();
}
class Dog implements IAnimal {
public function makeSound() {
echo "Woof..";
}
}
class Cat implements IAnimal {
public function makeSound() {
echo "Meowww..";
}
}
class NullAnimal implements IAnimal {
public function makeSound() {
// silence...
}
}
$animalType = 'elephant';
switch($animalType) {
case 'dog':
$animal = new Dog();
break;
case 'cat':
$animal = new Cat();
break;
default:
$animal = new NullAnimal();
break;
}
$animal->makeSound(); // ..the null animal makes no sound
function Exemplo(IAnimal $animal) {
$animal->makeSound();
}
I put in the Github for future reference.
There are cases that you need to treat the condition in a special way. Then you end up giving almost the same as using a null
. Only that there is separation of the code, which is good for some and bad for others. Then you would have to do the following:
function Exemplo(NullAnimal $animal) {
echo "não para para fazer nada com este objeto";
}
I would think of something like making an exception, if it were in the case of the example that the guy posted on SOEN
It’s a possibility but the idea of the pattern is not quite this since it would end up doing the same thing that would happen if it used a null
. I’m not saying that it has no advantage, but that’s not the idea. I would keep using null
in most situations. Although I would avoid situations that need a null
to give some special semantics to the object.
When I see this Pattern I remember video cards. It’s as if the games have an interface that requires the implementation of resources. This Nullobject class would be like a simulated graphics card (or a computer that doesn’t have a graphics card, and the VGA would be like
Translating my way: It is a way to make objects meet the requirements of an interface, without performing any operation with contract methods :)
No, because that would be saying that the video card doesn’t do anything. In fact the simulated video card would have to do some, reproduce the expected behavior. The second statement is true.
I think I miss playing on the pc :). That statement traveled - the first. But if the second is right, then I understood the line of reasoning.
Browser other questions tagged oop pattern-design
You are not signed in. Login or sign up in order to post.
I do not know with details, but I would think that it has the same purpose of the "neutral elements" in addition and multiplication (
0
and1
), or perhaps the "identity matrix": when your code needs to do an operation, but you don’t want anything to happen (but without having to create aif
to deal with this special case), you use this value "which is useless". It’s a bit like the "no-op" functions (no operation), which exist only to fill a hole. The interesting thing in these cases is that the code is identical there is some useful operation happening or not.– mgibsonbr