Origin: |
Woolf 97 |
Reason: |
To represent a null reference (a non-existent object)
with an (existing) object to avoid handling special cases.
|
Synopsis: |
Define a special class generally with no state, but the same
methods expected in the object that the Null Object
stands for.
|
Example: |
Objects in a sequence have a reference to the next object.
If the last object of the sequence has a null reference,
a test is required before invoking a method of the referenced
object. A Null Object eliminates the need for a test.
|
Solution: |
Delegator |
This class delegates an operation to objects that
normally may not exist.
|
AbstractOperation |
Superclass or interface of the objects that may be null.
|
RealOperation |
Implements the operation that the Delegator
delegates to AbstractOperation.
|
NullOperation |
Implements a do-nothing operation that the Delegator
delegates to AbstractOperation.
|
|
See also: |
Singleton
(the null object is often stateless)
Strategy
(the Null Object pattern may ease the
design of the Strategy pattern)
|