| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Null_object_pattern |
In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior (or lack thereof), was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object".
Here, the idea is that there are situations where a pointer or reference to an Animal object is required, but there is no appropriate object available. A null reference is impossible in standard-conforming C++. A null Animal* pointer is possible, and could be useful as a place-holder, but may not be used for direct dispatch: a->makeSound() is undefined behavior if a is a null pointer.
The null object pattern solves this problem by providing a special NullAnimal class which can be instantiated bound to an Animal pointer or reference.
The special null class must be created for each class hierarchy that is to have a null object, since a NullAnimal is of no use when what is needed is a null object with regard to some Widget base class that is not related to the Animal hierarchy.
Note that NOT having a null class at all is an important feature, in contrast to languages where "anything is a reference" (e.g., Java and C#). In C++, the design of a function or method may explicitly state whether null is allowed or not.
| Creational | |
|---|---|
| Structural | |
| Behavioral |
| View original on Wikipedia | Back | FazBrowse Home |