Null Object Design Pattern

Null Object design pattern – alternatively returning null object instead of ‘Null to avoid repeatedly checking for null values within code.

In the object oriented programming realm it's very common to create or use methods and properties that return null. And this resulting in NullReferenceException if the programmer forget to assert for null.

Null Object design pattern suggests to always return a valid instance instead of null. Rather returning null, you would return an object with a "null" or default behavior/state. This is the same approach as returning an default object instead of null. When you return null, the callers need to check for null. When you return an default object, the callers can access it without a problem - even if they forget to check for null.

image Consider a case of Member class which has Plan property and GetMember(id) method.image Now to access membership plan for current member our code goes somewhat like this:

 

member = GetMember(id);
currentMemberPlan = (member != null)
? member.Plan : PlanType.General;

But by implementing Null Object design pattern our code can become like this:

currentMemberPlan = GetMember(id).Plan
 

Implementation

  1. Create a subclass of the source class to act as a null version of the class. Create an isNull operation on the source class and the null class. For the source class it should return false, for the null class it should return true.
  2. Find all places that can give out a null when asked for a source object. Replace them to give out a null object instead.
  3. Look for cases in the parent class and override those behaviour in the null version of the class to do some alternative/default behavior.

image

In .Net(C# or VB) we can do this by implementing INullable interface.

Also we need to modify our GetMember(id) method of Member class to return null object(instance of NullMember class) instead of null value.

Also we should override members of Member class in NullMember class for some default behavior.

In this example, GetMember() never returns null, but instead an instance of NullMember (or DefaultCustomer or MissingCustomer), which is a sub-class of the Member class and whose Plan property is defined as follows:

 

       public override PlanType Plan
{
get
{
return PlanType.General;
}
set
{
//todo
}
}

The essence of this is that instead of checking an object for null and then invoking some behavior based on the answer, you just invoke the behavior. The object, depending on its type, does the right thing.

In .Net Framework, String.Empty is one of the most common, simple and best example to understand Null Object design pattern.

One more example:

Let's consider a call to a method that returns a Tool. Sometimes, the result will be a Hammer, sometimes a Wrench, or a ScrewDriver, etc. When you call the Use() method on each of these tools, you get a different result.

Now, if the method returns a NullTool, calling the Use() method would result in no action done at all. All this without having to test for null before the call to Use() and everywhere else when you pass the Tool around in the code.

Additional Links