Null Object: Doing Nothing Well
Null Object Players
This pattern has four players:
- Productive Object — the object that does the real work.
- Null Object — (A.K.A. Slacker) the object that provides a do-nothing version of the Productive Object.
- Caller — invokes methods on a Null Object or Productive Object without knowing which is being used.
- Configurator — configures a Caller to work with a Null Object or Productive Object.
One way to implement a Null Object is to introduce a subclass that does nothing well.
In the example on your right, NullMouseEventHandler
implements a null or do-nothing
alternative to the Productive Object, MouseEventHandler
.At runtime, a Configurator ensures that a Caller
always has a valid mouse event handler to call, whether it is an instance of a NullMouseEventHandler
or
a MouseEventHandler
.
Another way to make a Null Object is to introduce a superclass that does nothing well.
This time, NullMouseEventHandler
implements a null or do-nothing
superclass of the Productive Object, MouseEventHandler
.At runtime, a Configurator ensures that a Caller always has a valid
mouse event handler to call, whether it is an instance of a NullMouseEventHandler
or
a MouseEventHandler
.
Yet another way to produce a Null Object is to make it implement the same interface as
the Productive Object.
NullMouseEventHandler
is now a do-nothing class that implements the MouseEventHandler
interface.At runtime, a Configurator ensures that a Caller always has a valid mouse event handler to call,
whether it is an instance of a NullMouseEventHandler
or a MouseEventHandlerStandard
.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |