Null Object pattern
From Wikipedia, the free encyclopedia
In object-oriented computer programming, a null object is a behavioral pattern designed to act as a default value of an object.
Contents |
[edit] Motivation
In some object-oriented languages, such as Java, variables may be null. As these variables do not have methods, they cannot be used and variables may need to be checked to ensure they are not null. This tends to make code less readable and introduces duplicate code, so a special class of variables, with an identical interface are created to which variables of the class can be initialised.
It can also be used to act as a stub for testing, if a certain feature, such as a database, is not available for testing.
[edit] Relation to other patterns
It can be regarded as a special case of the State pattern.
It is not a pattern from Design Patterns, but is mentioned in Martin Fowlers Refactoring[1] and Joshua Kerievskys[2] book on refactoring in the Insert Null Object refactoring.
[edit] References
- ^ *Fowler, Martin (1999). Refactoring. Improving the Design of Existing Code. Addison-Wesley. ISBN 0-201-48567-2.
- ^ *Kerievsky, Joshua (2004). Refactoring To Patterns. Addison-Wesley. ISBN 0-321-21335-1.