Null Object Pattern

Null Object Pattern:

  • In NULL object Pattern, Null object replaces check of  Null object instances, 
  • Logic for dealing with a null field or variable is duplicated throughout your code.
  • Replace the null logic with a Null Object, an object  that provides the appropriate null behavior. 
  • Method should not return Null at any circumstances while invoking it,Instead It should return Null object.So that Caller Can avoid checking null logic.

Essence of Polymorphism:

  • The essence of polymorphism is that instead of asking an object what type it is and then invoking some behavior based on the answer, you just invoke the behavior.
  • The object, depending on its type, does the right thing. One of the less intuitive places to do this is where you have a null value in a field. 

Real Time Scenario:

  • If client calls any method  and it  returns some object . Before using that object we will do null check with that object,
  • But Null object Pattern says that We should avoid such null check.Instead We directly have to use that object by trusting it.

Sample Code:

       
if(customer!=null){
   name=customer.getName();
}else{
   name="unKnown"
}
             
Once We Implemented NULL Object Pattern, Then We can avoid some sort of unneccessary if condition codes.

       
   name=customer.getName();
             

LikeWise For Collections:

Instead of returning NULL,return empty collection like below.


List Set Map
Collections.emptyList() Collections.emptySet() Collections.emptyMap()

Note: The above methods always returns same immutable empty collections everytime it needs to return an empty collection.So need to worry about the multiple creation of objects.

Example:


ICustomer.java
1:  public interface ICustomer {  
2:        String getName();  
3:        Boolean isNull();  
4:  }  


Customer.java
1:  public class Customer implements ICustomer{  
2:       String name;  
3:       public Customer(String name){  
4:            this.name=name;  
5:       }  
6:       @Override  
7:       public Boolean isNull(){  
8:            return false;  
9:       }  
10:       @Override  
11:       public String getName(){  
12:            return name;  
13:       }  
14:  }  


NullCustomer.java
1:  public class NullCustomer implements ICustomer {  
2:       private static NullCustomer nullCustomer=new NullCustomer();  
3:       private NullCustomer(){  
4:       }  
5:       public NullCustomer getInstance(){  
6:            return nullCustomer;  
7:       }  
8:       @Override  
9:       public String getName() {  
10:            return "UnKnown";  
11:       }  
12:       @Override  
13:       public Boolean isNull() {  
14:            return true;  
15:       }  
16:  }  


CustomerFactory.java
1:  public class CustomerFactory {  
2:       public ICustomer getCustomer(String name){  
3:            ICustomer customer;  
4:            if(name.equalsIgnoreCase("ganesh")){  
5:                 customer=new Customer(name);  
6:            }else{  
7:                 customer=NullCustomer.getInstance();  
8:            }  
9:            return customer;  
10:       }  
11:  }  


If any Client access the getCustomer(String name) method,It either returns Customer object or NullCutomer, there is no possibility to get Null Object.

So Method invoker  can avoid if condition by checking the object is Null or not.

One more Advantage is that eventhough if he forgets it, there is no possibility of Null Pointer Exception.

But In Some Situation, We will identity the flow based on  the returned object.For that purpose I introduced isNull() function in the ICustomer,


  • For Customer object it returns false 
  • For NullCustomer object it returns true.
And also I made the NullCustomer Class as Singleton Class. Because Its state never going to change and also we were avoided multiple creation of objects.


 ICustomer customer=CustomerFactory.getCustomer("ganesh");  
 if(customer.isNull()){  
 //doAlternativeBehaviour  
 }else{  
 //doOrginal Behaviour  
 }  

Note: Still there is no removal of null check with the code  but avoided the possibility of Null Pointer Exception.


Additional Refractoring with Null Object Pattern:

Lets us consider that we already developed the  project and we are not interested to touch the existing Customer class to add any extra code,

In this Situation,Is it possible to implement the Null Object Pattern.

Yess!!!!!!!! We can do it with the help of Testing Interface Concept.

Create one marker Interface NullValue,

 public interface NullValue{  
 }  

NullObject Pattern should implement above interface too and replace the Customer.isNull() with the below code.

 ICustomer customer=CustomerFactory.getCustomer("ganesh");  
 if(customer instanceof NullValue){  
 //doAlternativeBehaviour  
 }else{  
 //doOrginal Behaviour  
 }  

Everthing done!!!!!!!!

Now We can easily differenciate the object.


Share this

Related Posts

Previous
Next Post »