English 中文(简体)
Design Patterns - Null Object Pattern
  • 时间:2024-12-22

Design Patterns - Null Object Pattern


Previous Page Next Page  

In Null Object pattern, a null object replaces check of NULL object instance. Instead of putting if check for a null value, Null Object reflects a do nothing relationship. Such Null object can also be used to provide default behaviour in case data is not available.

In 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 seemlessly where we need to check null value.

Implementation

We are going to create a AbstractCustomer abstract class defining opearations. Here the name of the customer and concrete classes extending the AbstractCustomer class. A factory class CustomerFactory is created to return either RealCustomer or NullCustomer objects based on the name of customer passed to it.

NullPatternDemo, our demo class, will use CustomerFactory to demonstrate the use of Null Object pattern.

Null Object Pattern UML Diagram

Step 1

Create an abstract class.

AbstractCustomer.java

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

Step 2

Create concrete classes extending the above class.

RealCustomer.java

pubpc class RealCustomer extends AbstractCustomer {

   pubpc RealCustomer(String name) {
      this.name = name;		
   }
   
   @Override
   pubpc String getName() {
      return name;
   }
   
   @Override
   pubpc boolean isNil() {
      return false;
   }
}

NullCustomer.java

pubpc class NullCustomer extends AbstractCustomer {

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

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

Step 3

Create CustomerFactory Class.

CustomerFactory.java

pubpc class CustomerFactory {
	
   pubpc static final String[] names = {"Rob", "Joe", "Jupe"};

   pubpc 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 the CustomerFactory to get either RealCustomer or NullCustomer objects based on the name of customer passed to it.

NullPatternDemo.java

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

      AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob");
      AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob");
      AbstractCustomer customer3 = CustomerFactory.getCustomer("Jupe");
      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

Verify the output.

Customers
Rob
Not Available in Customer Database
Jupe
Not Available in Customer Database
Advertisements