Files
abstract-document
abstract-factory
adapter
aggregator-microservices
api-gateway
async-method-invocation
bridge
builder
business-delegate
caching
callback
chain
command
composite
dao
data-mapper
decorator
delegation
dependency-injection
double-checked-locking
double-dispatch
event-aggregator
event-driven-architecture
execute-around
facade
factory-kit
factory-method
feature-toggle
fluentinterface
flux
flyweight
front-controller
half-sync-half-async
hexagonal
intercepting-filter
interpreter
iterator
layers
lazy-loading
mediator
memento
message-channel
model-view-controller
model-view-presenter
monad
monostate
multiton
mute-idiom
mutex
naked-objects
null-object
etc
src
README.md
pom.xml
object-pool
observer
page-object
poison-pill
private-class-data
producer-consumer
promise
property
prototype
proxy
publish-subscribe
reactor
reader-writer-lock
repository
resource-acquisition-is-initialization
semaphore
servant
service-layer
service-locator
singleton
specification
state
step-builder
strategy
template-method
thread-pool
tolerant-reader
twin
value-object
visitor
.gitignore
.travis.yml
CODE_COVERAGE.md
CONTRIBUTING.MD
LICENSE.md
README.md
checkstyle-suppressions.xml
checkstyle.xml
exclude-pmd.properties
faq.md
pom.xml
update-ghpages.sh
java-design-patterns/null-object/README.md

31 lines
1.1 KiB
Markdown
Raw Normal View History

---
layout: pattern
title: Null Object
folder: null-object
permalink: /patterns/null-object/
categories: Behavioral
2015-12-28 15:52:44 +02:00
tags:
- 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](./etc/null-object.png "Null Object")
## Applicability
Use the Null Object pattern when
* you want to avoid explicit null checks and keep the algorithm elegant and easy to read.
## Credits
* [Pattern Languages of Program Design](http://www.amazon.com/Pattern-Languages-Program-Design-Coplien/dp/0201607344/ref=sr_1_1)