Null Object Design Pattern

Introduction

 
In this article, we discuss the null object design pattern. 
 

Why do we need this pattern?

 
We all are familiar with null checks. The greater the number of null checks, the more the cyclomatic complexity. With the help of a null object design pattern, we can finally start bringing some sense to our null checks.
 
Let's start with a client class, which keeps the track of which smartPhone to sell. If SomeSmartPhoneMethod() returns nothing then SmartPhone class's object will be null.
 
In this case, the code might throw a null exception to the user.
  1. class SmartPhoneSale    
  2.  {    
  3.      SmartPhone OnePlus8 = SomeSmartPhoneMethod();    
  4.           
  5.      static void Main(string[] args)    
  6.      {    
  7.          if (OnePlus8 == null)    
  8.          {    
  9.              throw new ArgumentNullException();    
  10.          }    
  11.      }    
  12.  }     
Let's see how it looks conceptually:
 
Null Pointer Design Pattern
 
As per our concept, we have already implemented a client class: SmartPhoneSale.
 
Now we need abstraction in our code. Let's go ahead and create that interface. 
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4.     
  5. namespace NullObjectDesignPattern    
  6. {    
  7.     public interface ISmartPhone    
  8.     {    
  9.         string Name { get; }    
  10.     
  11.         double Price { get; }    
  12.     }    
  13. }     
We need to concrete classes. One is for default initialization and another for our logic.
 
Let's create default class.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4.     
  5. namespace NullObjectDesignPattern    
  6. {    
  7.     class DefaultSmartPhone : ISmartPhone    
  8.     {    
  9.     
  10.         public string Name { get => "Please select smart phone first"; }    
  11.         public double Price { get => 0; }    
  12.     }    
  13. }     
As you can see, we are doing nothing here, other than just assigning an object to do something when it goes into null state.
 
Basic implementation
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4.     
  5. namespace NullObjectDesignPattern    
  6. {    
  7.     public class SmartPhone : ISmartPhone    
  8.     {    
  9.         public SmartPhone(string Name, double price)    
  10.         {    
  11.             this.Name = Name;    
  12.             this.Price = price;    
  13.         }    
  14.         public string Name { get; }    
  15.     
  16.         public double Price { get; }    
  17.     }    
  18. }     
We are all set, Now let's make a few changes in the client class:
  1. using System;    
  2.     
  3. namespace NullObjectDesignPattern    
  4. {    
  5.     class SmartPhoneSale    
  6.     {    
  7.         private static SmartPhone SmartPhoneInstance;//= new SmartPhone("OnePlus8Pro", 55000);    
  8.                
  9.         static void Main(string[] args)    
  10.         {    
  11.             ISmartPhone BrandNewPhone = SmartPhoneInstance;    
  12.             if (BrandNewPhone == null)    
  13.             {    
  14.                 BrandNewPhone = new DefaultSmartPhone();    
  15.             }    
  16.             Console.WriteLine($"Phone: {BrandNewPhone.Name},  Price: {BrandNewPhone.Price}");    
  17.         }    
  18.     }    
  19. }     
As you can see, when the object for smartPhone is null, we are assigning him default class's instance.
 
Beautiful.
 
Output when you did not initialize a smartPhoneInstance:
 
Null Pointer Design Pattern
 
Output when you initialize a SmartPhoneInstance. Just uncomment the line above.
 
Null Pointer Design Pattern
 
This is the simplest design pattern to implement. It is a good practice to work with design patterns. It gives you a broad idea on object communications.
 
Feel free to download the attached source code for your reference.
 
I sincerely hope that you enjoyed this blog and that you're inspired to apply what you've learned to your own applications.
 
Happy coding.
 
Connect with me: