Problem

Conditional code adds bloat and decreases speed as the logic branches further and further.

Solution

We create a object that implements the desired interface and helps to eliminate conditional code by defining the behavior by defining a default behavior for an empty body.

Related Patterns

Discussion

The Null Object is very predictable and allows us to test certain conditions without explicitly checking them in a conditional. It's useful precisely because it does nothing.

Examples

We can iterate through a linked list. The last node in the chain will be a Null node. When we access it, it halts the iteration process. That way, we don't have to check each node to see if it's null, the standard way to check the existence. The end-of-list behavior we are trying to create is defined in our Null Object, therefore eliminating the need for costly conditionals.

Code

This Null List allows us to visit the entire list (See Visitor Pattern). If we don't use the Null Object, we would get many NullObjectExceptions. With it, the Null Object itself knows what to do.

public abstract class List{
  public abstract List getTail();
}

public class LList extends List{
  private Object head;
  private Object next;
  public LList(Object head, Object next){
    this.head = head;
    this.next = next;
  }
  public Object getHead(){return head;}
  public List getNext(){return next;}
}
public NullList extends List{
  private static final NullList instance = new NullList();
  private NullList(){}
  public static NullList Singleton(){return instance;}
  public List getNext(){return this;}
}