Zero object (design pattern)

from Wikipedia, the free encyclopedia

A null object is a design pattern (English null object pattern ) belonging to the behavior pattern category . The null object design pattern applies to disabling references to variables and consists of assigning the reference to an object that takes no action, rather than invalidating the reference. This ensures that the reference to the variable refers to a valid object at any point in time, which makes handling of special cases (the non-existence) unnecessary.

advantages

  • Code duplication is avoided.
  • Errors are prevented if no check has taken place.

disadvantage

  • A null object that is to take the place of the value null must implement the public methods of the corresponding interface.

variant

  • The zero object can be implemented as a singleton (single item). This variant uses less memory, since only one instance of the zero object is instantiated.

literature

  • Martin Fowler: Refactoring. How to improve the design of existing software . Addison-Wesley, Munich 2000, ISBN 3-8273-1630-8 , pp. 264–272 (English: Refactoring. Improving The Design Of Existing Code . Translated by Bernd Kahlbrandt).