Easy Tutorial
❮ Composite Pattern Builder Pattern ❯

Null Object Pattern

In the Null Object Pattern, a null object replaces the checking of NULL object instances. Instead of checking for a null value, the Null Object reflects a do-nothing relationship. Such a Null Object can also provide default behavior when data is not available.

In the Null Object Pattern, we create an abstract class specifying various operations to be done, concrete classes extending this class, and a null object class providing do-nothing implemention of this class and will be used seamlessly where we need to check null value.

Implementation

We'll create an AbstractCustomer abstract class defining operations (here, it's the customer's name), and concrete classes extending the AbstractCustomer class. The CustomerFactory factory class returns either RealCustomer or NullCustomer objects based on the name passed to it.

NullPatternDemo, our demo class, uses CustomerFactory to demonstrate the use of the Null Object Pattern.

Step 1

Create an abstract class.

AbstractCustomer.java

public abstract class AbstractCustomer {
   protected String name;
   public abstract boolean isNil();
   public abstract String getName();
}

Step 2

Create concrete classes extending the above class.

RealCustomer.java

public class RealCustomer extends AbstractCustomer {

   public RealCustomer(String name) {
      this.name = name;    
   }

   @Override
   public String getName() {
      return name;
   }

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

NullCustomer.java

public class NullCustomer extends AbstractCustomer {

   @Override
   public String getName() {
      return "Not Available in Customer Database";
   }

   @Override
   public boolean isNil() {
      return true;
   }
}

Step 3

Create the CustomerFactory class.

CustomerFactory.java

public class CustomerFactory {

   public static final String[] names = {"Rob", "Joe", "Julie"};

   public static AbstractCustomer getCustomer(String name){
      for (int i = 0; i < names.length; i++) {
         if (names[i].equalsIgnoreCase(name)){
            return new RealCustomer(name);
         }
      }
      return new NullCustomer();
   }
}

Step 4

Use CustomerFactory to get either RealCustomer or NullCustomer objects based on the name passed.

NullPatternDemo.java

public class NullPatternDemo {
   public static void main(String[] args) {

      AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob");
      AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob");
      AbstractCustomer customer3 = CustomerFactory.getCustomer("Julie");
      AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura");

      System.out.println("Customers");
      System.out.println(customer1.getName());
      System.out.println(customer2.getName());
      System.out.println(customer3.getName());
      System.out.println(customer4.getName());
   }
}

Step 5

Execute the program, outputting the result:

Customers
Rob
Not Available in Customer Database
Julie
Not Available in Customer Database
❮ Composite Pattern Builder Pattern ❯