FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Null_object_pattern

Null object pattern

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior (or lack thereof), was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object".

importstd; usingstd::unique_ptr; classAnimal{ public: virtual~Animal()=default; virtualvoidmakeSound()const=0; }; classDog:publicAnimal{ public: virtualvoidmakeSound()constoverride{ std::println("Woof!"); } }; classNullAnimal:publicAnimal{ public: virtualvoidmakeSound()constoverride{ // silence... } }; intmain(){ unique_ptr<Animal>dog=std::make_unique<Dog>(); dog->makeSound();// outputs "Woof!" unique_ptr<Animal>unknown=std::make_unique<NullAnimal>(); unknown->makeSound();// outputs nothing, but does not throw a runtime exception }

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->makeSound() is undefined behavior if a is a null pointer.

The null object pattern solves this problem by providing a special NullAnimal 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 NullAnimal 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.

Note that NOT having a null class at all is an important feature, in contrast to languages where "anything is a reference" (e.g., Java and C#). In C++, the design of a function or method may explicitly state whether null is allowed or not.

classDog{ sound(){ return'bark'; } } classNullAnimal{ sound(){ returnnull; } } functiongetAnimal(type){ returntype==='dog'?newDog():newNullAnimal(); } ['dog',null].map((animal)=>getAnimal(animal).sound()); // Returns ["bark", null]

Java

edit
Woolf, Bobby (1998). "Null Object". In Martin, Robert C.; Riehle, Dirk; Buschmann, Frank (eds.). Pattern Languages of Program Design 3 (PDF). Addison-Wesley. ISBN 978-0201310119. Retrieved 23 September 2026.{{cite book}}: Check |isbn= value: checksum (help)
  • ↑ Martin, Robert (2002). Agile Software Development: Principles, Patterns and Practices. Pearson Education. ISBN 0-13-597444-5.
  • ↑ Fowler, Martin (1999). Refactoring, p. 216.
  • External links

    edit
    Nulls in computing
    Software design patterns
    Gang of Four
    patterns
    Creational
    Structural
    Behavioral
    Concurrency
    patterns
    Architectural
    patterns
    Other
    patterns
    Books
    People
    Communities
    See also

    View original on Wikipedia | Back | FazBrowse Home