BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Null Object Design Pattern

The null object pattern is a design pattern that simplifies the use of dependencies that can be undefined. This is achieved by using instances of a concrete class that implements a known interface, instead of null references.

What is the Null Object Pattern?

When you develop classes that have dependencies, you will sometimes have cases where a dependency is not needed, perhaps because the normal behaviour of the dependency is not required. One way to deal with such a situation is to use a null reference instead of a real object for that dependency. Unfortunately, using null tends to complicate the code. Wherever you need to use a member from the object, you must first check if it is null, as calling a method or accessing a property of a null reference will cause an exception.

The Null Object pattern provides a solution to this problem. Instead of passing null references and checking for the presence of null before any operation, you create a specific class that represents a non-functional dependency. This class implements an expected interface or inherits from an abstract class but includes no functionality. Its methods and properties perform no action and return default, fixed values. This allows any dependant object to use null object dependencies without performing any pre-checks, simplifying the code.

The null object pattern is usually used with other design patterns. The null object class itself is often created as a Singleton. This limits the number of instances to one, which is ideal as null objects generally have no state and no functionality so creating additional objects would add unnecessary overhead to your software. Another design pattern that is often found with the null object pattern is the strategy pattern. When one of the strategies requires no functionality, a null object can be used. A third common associated pattern is the factory method pattern, where the factory may return a null object instance.

An example of the use of the null object pattern could be a system that reports status messages. It may be that you want such software to send a message in response to an event. Using the strategy pattern, you could supply dependencies for various options for reporting the status, such as delivering an email or logging information via a remote service. If in some situations you do not need to report the status at all, a third strategy option could be controlled with a null object.

Implementing the Null Object Pattern

Null Object Design Pattern UML

The UML class diagram above describes an implementation of the null object pattern. The items in the diagram are described below:

  • Client. This class has a dependency that may or may not be required. Where no functionality is required in the dependency, it will execute the methods of a null object.
  • DependencyBase. This abstract class is the base class for the various available dependencies that the Client may use. This is also the base class for the null object class. Where the base class provides no shared functionality, it may be replaced with an interface.
  • Dependency. This class is a functional dependency that may be used by the Client.
  • NullObject. This is the null object class that can be used as a dependency by the Client. It contains no functionality but implements all of the members defined by the DependencyBase abstract class.

The following shows the basic code for the null object pattern, implemented using C#. Here the Client class accepts the dependency using constructor injection.

public class Client
{
    DependencyBase _dependency;

    public void Client(DependencyBase dependency)
    {
        _dependency = dependency;
    }

    public void DoSomething()
    {
        _dependency.Operation();
    }
}


public abstract class DependencyBase
{
    public abstract void Operation();
}


public class Dependency : DependencyBase
{
    public override void Operation()
    {
        Console.WriteLine("Dependency.Operation() executed");
    }
}


public class NullObject : DependencyBase
{
    public override void Operation() { }
}
8 September 2012