Friday, January 31, 2014

Behavioral DP : NULL Object Design Pattern



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 behavior in case data is not available.
In Null Object pattern, we create a abstract class specifying the various operations to be done, concrete classes extending this class and a null object class providing do nothing implementation of this class and will be used seamlessly where we need to check null value

Implementation

We're 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 use of Null Object pattern.
 
1:  public abstract class AbstractCustomer {  
2:       protected String name;  
3:       public abstract boolean isNil();  
4:       public abstract String getName(String name);  
5:  }  
6:  public class NullCustomer extends AbstractCustomer {  
7:       @Override  
8:       public String getName(String name) {  
9:            return name+ " Not Available in Customer Database";  
10:       }  
11:       @Override  
12:       public boolean isNil() {  
13:            return true;  
14:       }  
15:  }  

 Real Customer to check the value
1:  public class RealCustomer extends AbstractCustomer {  
2:       public RealCustomer(String name) {  
3:        this.name = name;  
4:       }  
5:       @Override  
6:       public String getName(String name) {  
7:            return name+ " Available in Customer Database";  
8:       }  
9:       @Override  
10:       public boolean isNil() {  
11:            return false;  
12:       }  
13:  }}  

 Sample example with help of factory 

1:  public class CustomerFactory {  
2:        public static final String[] names = {"Rob", "Joe", "Julie"};  
3:         public static AbstractCustomer getCustomer(String name){  
4:          for (int i = 0; i < names.length; i++) {  
5:            if (names[i].equalsIgnoreCase(name)){  
6:             return new RealCustomer(name);  
7:            }  
8:          }  
9:          return new NullCustomer();  
10:         }  
11:  }  

 Demo tutorial

1:  public class NullPatternDemo {  
2:       public static void main(String[] args) {  
3:             AbstractCustomer customer1 = CustomerFactory.getCustomer("Rob");  
4:          AbstractCustomer customer2 = CustomerFactory.getCustomer("Bob");  
5:          AbstractCustomer customer3 = CustomerFactory.getCustomer("Julie");  
6:          AbstractCustomer customer4 = CustomerFactory.getCustomer("Laura");  
7:          System.out.println("……Customers……");  
8:          System.out.println(customer1.getName("Rob"));  
9:          System.out.println(customer2.getName("Bob"));  
10:          System.out.println(customer3.getName("Julie"));  
11:          System.out.println(customer4.getName("Laura"));  
12:       }  
13:  }  


No comments: