SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Null Object pattern

rated by 0 users
This post has 2 Replies | 1 Follower

Top 25 Contributor
Posts 232
Thanh Posted: 08-28-2005 8:39 AM

Null object is an object that has no state and does nothing. It acts as a surrogate for the lack of an object of a given type.

It can be used from the client to prevent the nasty null object reference at runtime in case clients forget to check for null, therefore, it's a very smart null object.

using System;

namespace Test
{
  public class Customer
  {
    private int _id;
    public int Id
    {
      get { return _id; }
      set { _id = value; }
    }
   
    private string _name;
    public string Name
    {
      get { return _name; }
      set { _name = value; }
    } 
   
    public Customer(int Id, string Name)
    {
      this.Id = Id;
      this.Name = Name;
    }
  }
 
  public abstract class BaseObject
  {
    public abstract object GetCustomer();
  }
 
  public class NonNullObject : BaseObject
  {
    private object Customer;
       
    public NonNullObject(object Customer)
    {
      this.Customer = Customer;
    }   
   
    public override object GetCustomer()
    {
      return (object)Customer;
    }       
  }
 
  public class NullObject : BaseObject
  {
    private static NullObject Instance = new NullObject();
   
    public static NullObject GetInstance()
    {
      return Instance;
    }
   
    public override object GetCustomer()
    {     
      Console.WriteLine("Null object has no state and does nothing");
      return this;
    }
  }
 
  public class Context
  {
    private BaseObject Strategy;
   
    public Context(BaseObject Strategy)
    {
      this.Strategy = Strategy;
    }
   
    public Object GetCustomer()
    {
      return Strategy.GetCustomer();
    }   
  }
 
  public class Test
  {
    public static void Main(string[] Args)
    {
      Customer Cust = new Customer(1, "Thanh");
     
      Context obj = new Context(new NonNullObject(Cust));
      Customer Temp = (Customer)obj.GetCustomer();
      Console.WriteLine("Id: {0} Name: {1}", Temp.Id.ToString(), Temp.Name);
     
      obj = new Context(NullObject.GetInstance());     
      obj.GetCustomer();     
    }
  }
}

1. Create the abstract base class or interface
2. Create a child class that inherits from the base class. Instantiating itself as a singleton and provide a method for clients to get an instance of it.
3. Clients can now treat the null object as any other valid object avoiding the nasty null object reference at runtime.

Hope it helps

Top 10 Contributor
Posts 2,284
Did you read what Martin Fowler had to say on that? Sometimes things go so smoothly that "vaccumm object" ends up in unexpected places. Which brings the question: shouldn't client check for null, lest they think valid operations are happening, which may cause havoc for not being aware?

(e.g. adding, subtracting money from an Account object implementing the Null pattern)

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 25 Contributor
Posts 232
No, I missed that section. Thanks for the point. It's although a valid point to always have to consider before this pattern can be properly used by the client. As you mention that "vacuum object" ends up in unexpected places, especially in critical areas like accounting dealing with money should not have valid null object returned to client. Therefore, a null reference should be checked on the client to make sure that it's treated as an error. That's my 2c opinion. I also believe this job is the responsibility of developers and architects to make sure that this doesn't happen in the first place.
Page 1 of 1 (3 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems