Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Behavioral Patterns

Null Object

The intent of a Null Object is to encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. In short, a design where "nothing will come of nothing"

Example - Empty iterator instead of null

We are going to use null object to represent empty iterator. Then we use this iterator whenever we would want to return null.

interface Iterator<T> {
    boolean hasNext();
    T next();
}

class IteratorImpl<T> implements Iterator {

    @Override
    public boolean hasNext() {
        return true; // TODO: here would be checking if the collection has more items to iterate through
    }

    @Override
    public T next() {
        return null;
    }
}

class NullIterator<T> implements Iterator {

    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    public T next() {
        return null;
    }
}

Instead of returning or working with null, we return instance of NullIterator.

class MyCollection {
    private List list;

    public Iterator iterator() {
        if (list == null) {
            return new NullIterator();
        }
        return new IteratorImpl(); // we would have to finish implementation of this iterator
    }
}
PreviousInterpreterNextIterator

Last updated 5 years ago

Was this helpful?