Null checks are quite common in the code I write. It litters up my class methods with all kinds of exception handling. The Null Object Design Pattern helps me to avoid this checking for null. It provides a non functional object to the client instead of null. So I can call a method on this object that effectively does nothing. This is what I learned.
Create a base class (or interface) with an embedded null object, returning default value on null from a static read only property. Create a few objects derived from that base class and implement the abstract properties and methods. From your Repository return the Null Object instead of null whenever needed. In the example the Null Object gets returned on a find to the ‘Restless’ Person.
using System;
namespace NullObject
{
class Program
{
static void Main(string[] args)
{
PersonRepository personRepository = new PersonRepository();
var oldPerson = personRepository.Find("Old");
var youngPerson = personRepository.Find("Young");
var restlessPerson = personRepository.Find("Restless");
oldPerson.Talk();
youngPerson.Talk();
restlessPerson.Talk();
oldPerson.ShutUp();
youngPerson.ShutUp();
restlessPerson.ShutUp();
Console.Read();
}
}
public abstract class PersonBase
{
public abstract string Name { get; }
public abstract int Age { get; }
public abstract void Talk();
public abstract void ShutUp();
#region Null implementation
// Singleton
static readonly NullPerson nullPerson = new NullPerson();
public static NullPerson Null
{
get { return nullPerson; }
}
// Embedded Null Object class
public class NullPerson : PersonBase
{
public override string Name { get { return string.Empty; } }
public override int Age { get { return 0; } }
public override void Talk() { }
public override void ShutUp() { }
}
#endregion
}
public class OldPerson : PersonBase
{
public override string Name
{
get { return "Grandpa"; }
}
public override int Age
{
get { return 80; }
}
public override void Talk()
{
Console.WriteLine(
"Bladibla uche uche bla... I'm {0} years old", this.Age);
}
public override void ShutUp()
{
Console.WriteLine("{0} stopped talking", this.Name);
}
}
public class YoungPerson : PersonBase
{
public override string Name
{
get { return "Little girl"; }
}
public override int Age
{
get { return 7; }
}
public override void Talk()
{
Console.WriteLine(
"Bladibla hihihihihi bla... I'm {0} years old", this.Age);
}
public override void ShutUp()
{
Console.WriteLine("{0} stopped talking", this.Name);
}
}
public class PersonRepository
{
public PersonBase Find(string name)
{
if (name.Contains("Old"))
{
return new OldPerson();
}
else if (name.Contains("Young"))
{
return new YoungPerson();
}
return PersonBase.Null;
}
}
}
As expected, the find to a ‘RestlessPerson’ gets a null object returned and does nothing.