2015-08-13 23:54:40 +02:00
---
layout: pattern
title: Null Object
folder: null-object
2015-08-15 18:03:05 +02:00
permalink: /patterns/null-object/
2015-08-20 21:40:07 +02:00
categories: Behavioral
2015-12-28 15:52:44 +02:00
tags:
2019-12-13 21:09:28 +02:00
- Extensibility
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Intent
In most object-oriented languages, such as Java or C#, references
2015-08-13 23:54:40 +02:00
may be null. These references need to be checked to ensure they are not null
before invoking any methods, because methods typically cannot be invoked on
null references. Instead of using a null reference to convey absence of an
object (for instance, a non-existent customer), one uses an object which
implements the expected interface, but whose method body is empty. The
advantage of this approach over a working default implementation is that a Null
Object is very predictable and has no side effects: it does nothing.
2019-12-07 20:01:13 +02:00
## Class diagram
2015-08-13 23:54:40 +02:00

2016-01-03 21:14:30 +01:00
## Applicability
Use the Null Object pattern when
2015-08-13 23:54:40 +02:00
2019-12-13 21:09:28 +02:00
* You want to avoid explicit null checks and keep the algorithm elegant and easy to read.
2016-01-27 12:55:59 +05:30
## Credits
2019-12-13 21:09:28 +02:00
2020-07-06 13:31:07 +03:00
* [Pattern Languages of Program Design 3 ](https://www.amazon.com/gp/product/0201310112/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201310112&linkCode=as2&tag=javadesignpat-20&linkId=7372ffb8a4e39a3bb10f199b89aef921 )