Album Cover

Design Patterns Vol. 2 Java

Close Preview
The 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.
Variations
UML for Do Nothing Child
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.
UML for Do Nothing Parent
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.
UML for Do Nothing Sibling
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...)
| Tweet...
Page rating: | Rate this Page