Null Object pattern
Encyclopedia
In object-oriented computer programming
, a Null Object is an object with defined neutral ("null") behavior. The Null Object design pattern
describes the uses of such objects and their behavior (or lack thereof). It was first published in the Pattern Languages of Program Design book series.
or C#, references
may be null. These references need to be checked to ensure they are not null before invoking any methods
, because methods typically cannot be invoked on null references.
For example, a function may retrieve a list of files in a directory and perform some action on each. In the case of an empty directory, one response may be to throw an exception or return a null reference rather than a list. Thus, the code which expects a list must verify that it in fact has one before continuing, which can complicate the design.
By returning a null object (i.e. an empty list) instead, there is no need to verify that the return value is in fact a list. The calling function may simply iterate the list as normal, effectively doing nothing. It is, however, still possible to check whether the return value is a null object (e.g. an empty list) and react differently if desired.
The null object pattern can also be used to act as a stub for testing if a certain feature, such as a database, is not available for testing.
and the Strategy pattern
.
It is not a pattern from Design Patterns, but is mentioned in Martin Fowler's
Refactoring and Joshua Kerievsky's book on refactoring in the Insert Null Object refactoring
.
Chapter 17 is dedicated to the pattern in Robert Cecil Martin's
Agile Software Development: Principles, Patterns and Practices
to be safely executed two or more times.
Here, the idea is that there are situations where a pointer or reference to an
The null object pattern solves this problem by providing a code special
The special null class must be created for each class hierarchy that is to have a null object, since a
Any operation that fails to return a sensible object for its purpose may return
Since
The null object pattern is also supported in multiple value processing. If the program attempts to extract a value from an expression which returns no values, the behavior is that the null object
Thus
The class
Since
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, a Null Object is an object with defined neutral ("null") behavior. The Null Object design pattern
Design pattern
A design pattern in architecture and computer science is a formal way of documenting a solution to a design problem in a particular field of expertise. The idea was introduced by the architect Christopher Alexander in the field of architecture and has been adapted for various other disciplines,...
describes the uses of such objects and their behavior (or lack thereof). It was first published in the Pattern Languages of Program Design book series.
Motivation
In most object-oriented languages, such as JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
or C#, references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...
may be null. These references need to be checked to ensure they are not null before invoking any methods
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
, because methods typically cannot be invoked on null references.
Description
Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, but whose method body is empty. The advantage of this approach over a working default implementation is that a Null Object is very predictable and has no side effects: it does nothing.For example, a function may retrieve a list of files in a directory and perform some action on each. In the case of an empty directory, one response may be to throw an exception or return a null reference rather than a list. Thus, the code which expects a list must verify that it in fact has one before continuing, which can complicate the design.
By returning a null object (i.e. an empty list) instead, there is no need to verify that the return value is in fact a list. The calling function may simply iterate the list as normal, effectively doing nothing. It is, however, still possible to check whether the return value is a null object (e.g. an empty list) and react differently if desired.
The null object pattern can also be used to act as a stub for testing if a certain feature, such as a database, is not available for testing.
Relation to other patterns
It can be regarded as a special case of the State patternState pattern
The state pattern, which closely resembles Strategy Pattern, is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to represent the state of an object. This is a clean way for an object to partially change its type at...
and the Strategy pattern
Strategy pattern
In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable...
.
It is not a pattern from Design Patterns, but is mentioned in Martin Fowler's
Martin Fowler
-Online presentations:* at RailsConf 2006* at JAOO 2006* at QCon London 2007 * at QCon London 2008 * at ThoughtWorks Quarterly Technology Briefing, October 2008...
Refactoring and Joshua Kerievsky's book on refactoring in the Insert Null Object refactoring
Refactoring
Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior", undertaken in order to improve some of the nonfunctional attributes of the software....
.
Chapter 17 is dedicated to the pattern in Robert Cecil Martin's
Robert Cecil Martin
Robert Cecil Martin, known colloquially as "Uncle Bob", is an American software consultant and author. Martin has been a software professional since 1970 and an international software consultant since 1990. In 2001, he initiated the meeting of the group that created Agile software development from...
Agile Software Development: Principles, Patterns and Practices
C
In C, functions can be written such that they accept a null pointer without failing. For instance the standard functionfree
may be called with a null argument. This allows code liketo be safely executed two or more times.
C++
A language with statically typed references to objects illustrates how the null object becomes a more complicated pattern: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->make_sound
is undefined behavior if a
is a null pointer.The null object pattern solves this problem by providing a code special
null_animal
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
null_animal
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.C#
In C#, arrays are first class objects with methods and properties that are available as long as you have an array instance, no matter how many elements the array has, whether it be 0, 1, or 100 items. Zero-length arrays are an example of the null object pattern.Smalltalk
Following the Smalltalk principle, everything is an object, the absence of an object is itself modeled by an object, callednil
. In the GNU Smalltalk for example, the class of nil
is UndefinedObject
, a direct descendant of Object
.Any operation that fails to return a sensible object for its purpose may return
nil
instead, thus avoiding the special case of returning "no object". This method has the advantage of simplicity (no need for a special case) over the classical "null" or "no object" or "null reference" approach. Especially useful messages to be used with nil
are isNil
or ifNil:
, which make it practical and safe to deal with possible references to nil
in Smalltalk programs.Common Lisp
In Lisp, functions can gracefully accept the special objectnil
, which reduces the amount of special case testing in application code. For instance although nil
is an atom and does not have any fields, the functions car
and cdr
accept nil
and just return it, which is very useful and results in shorter code.Since
nil
is the empty list in Lisp, the situation described in the introduction above doesn't exist. Code which returns nil
is returning what is in fact the empty list (and not anything resembling a null reference to a list type), so the caller does not need to test the value to see whether or not it has a list.The null object pattern is also supported in multiple value processing. If the program attempts to extract a value from an expression which returns no values, the behavior is that the null object
nil
is substituted.Thus
(list (values))
returns (nil)
(a one-element list containing nil). The (values)
expression returns no values at all, but since the function call to list
needs to reduce its argument expression to a value, the null object is automatically substituted.CLOS
In Common Lisp, the objectnil
is the one and only instance of the special class null
. What this means is that a method can be specialized to the null
class, thereby implementing the null design pattern. Which is to say, it is essentially built into the object system:The class
null
is a subclass of the symbol
class, because nil
is a symbol.Since
nil
also represents the empty list, null
is a subclass of the list
class, too. Methods parameters specialized to symbol
or list
will thus take a nil
argument. Of course, a null
specialization can still be defined which is a more specific match for nil
.Scheme
Unlike Common Lisp, and many dialects of Lisp, the Scheme dialect does not have a nil value which works this way; the functionscar
and cdr
may not be applied to an empty list; Scheme application code therefore has to use the empty?
or pair?
predicate functions to sidestep this situation, even in situations where very similar Lisp would not need to distinguish the empty and non-empty cases thanks to the behavior of nil
.