Null Object

Definition: A null object is an object that implements an interface but does nothing.

Usage: Null objects can be used when you don't have an object to return but don't want to return null because that would require that the method caller check for null.

Example

interface Command {
	do();
	undo();
}

class NoCommand {
	do() {}
	undo() {}
}

Null objects are sometimes useful when used in conjuction with the Command design pattern. Hence, the examples for the two patterns are related, though they don't necessarily have to be.

Index

Copyright (C) 2008-2009 Steven Fletcher. All rights reserved.