Skip to content

Files

Latest commit

dea2316 · Jan 1, 2018

History

History
This branch is 2178 commits behind iluwatar/java-design-patterns:master.

null-object

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 28, 2017
Dec 31, 2017
Nov 28, 2017
Jan 1, 2018
layout title folder permalink categories tags
pattern
Null Object
null-object
/patterns/null-object/
Behavioral
Java
Difficulty-Beginner

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.

alt text

Applicability

Use the Null Object pattern when

  • you want to avoid explicit null checks and keep the algorithm elegant and easy to read.

Credits