Null Object Design Pattern

What is Null Object Design Pattern?

  • It falls under behavioral design pattern.
  • It replaces “null” check.
  • Traditional method: null check for every value which has some limitations in code flow. If we follow “Null object design pattern” then we can get over that hurdle.
  • The default behavior for objects which has “null” or “nothing.
  • The null object class is the default implementation.
  • It would be handy if we want some special flow for something which has nothing or null value.
  • Example: Spam classification. 

Real Life Examples of Null Object Design Pattern

Example 1:

UML Class Diagram of Null Object Design Pattern

Null Object Design Pattern Java Implementation

Step 1:

public class Voltage
{

    private int voltage;

    public Voltage(int v)
    {
        this.voltage = v;
    }

    public int getVolts()
    {
        return voltage;
    }

    public void setVolts(int voltage)
    {
        this.voltage = voltage;
    }

}

Tutorial Videos of Null Object Design Pattern

For tutorial videos, please check out the premium Software Design Pattern course on Udemy.