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:
|
|
|
|
- Java
|
|
|
|
- Difficulty-Beginner
|
2015-08-13 23:54:40 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
**Intent:** In most object-oriented languages, such as Java or C#, references
|
|
|
|
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.
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
**Applicability:** Use the Null Object pattern when
|
|
|
|
|
2015-08-15 18:03:05 +02:00
|
|
|
* you want to avoid explicit null checks and keep the algorithm elegant and easy to read.
|