This article describes the Null Object design pattern. This is a behavioral design pattern, a category of design pattern used by software engineers, when writing computer programs.

Introduction

The Null Object pattern is a design pattern, used in software engineering, to provide a consistent return object in all circumstances. The assumption is that the "receiving" code processes a Null Object the same as it does for a normal object, however there will be no action as a result. Null Objects are basically classes that do nothing, or contain only rudimentary information to handle the object, like an empty Person class acting only as a stub.



It is defined as a behavioral design pattern, because the program continues to execute and process the data even if the only action is to "do nothing".

Benefits

This allows the developer to add less "boiler-plating" like checking for null values and ensures program execution continues in all circumstances. Stubbing return data with a Null Object means the developer can get on with the code and come back and fill in the details later. The developer can execute and test the application while working on layout considerations, then come back later to fill in some dummy data in a proper Person class.

Examples of the pattern

In C#, implementing an Interface can lead to many new methods that need writing. If it is not the time to concentrate on such things, you may wish to return a null object, to at least allow the compiler to build.

If you needed to retrieve and perform actions on a user, you may retrieve that information from a database. However you do not know if all the fields are available, so you would have to check for null before using a property. However, returning a Null Object allows code to just get on and use the property as it will at least be defined to it's default or initial state.

 

Return to Top


See Also

Link to domain parent articles and related articles in TechNet Wiki.

 

 

Return to Top


Community Resources

These are the external links, including links to Microsoft and TechNet sites that are non-Wiki
  • [Please contribute]

 

Return to Top


References section

Use this section if you pulled source material and ideas from other sites, blogs, or forums. Make sure you have permission from authors to use their material.
  • [Please contribute]

 

 

Return to Top