Home IT Info News Today IDG Contributor Network: How to handle null references using the…

IDG Contributor Network: How to handle null references using the…

235

A null object is one that doesn’t have any referenced value. The null object pattern is a behavioral design pattern that is used to provide a consistent return object in lieu of null. A NULL Object is just like any other object (they act just like a stub object) but it just contains rudimentary information and doesn’t do anything in particular. The NULL Object pattern is a popular pattern adopted to avoid writing too much boiler plate code when checking for null values.

Design patterns provide solutions to recurring problems and complexities in software designs. They are categorized into: Creational, Structural and Behavioural categories. Basically, the Behavioural design patterns deal with object collaboration and delegation of responsibilities. The NULL Object pattern falls under the behavioral design pattern category since the program flow isn’t stopped — the program’s flow continues as usual. In other words, the NULL Object pattern provides a “do-nothing” while at the same time preventing run-time exceptions in your application and enabling the program to flow as usual.

Be it a console application, a windows application or a web application, you may often encounter null reference exceptions in your code, isn’t it? In using the NULL Object pattern, you would have to write less boiler plate code like checking for null values as shown in the code snippet given below.

private List<Product> GetProductsByName(string category)

       {

          if (category == null) throw new ArgumentNullException("Category is null");

           //Write code here to retrieve products by category and return it

       }

You need to do similar checks many a time in your code. Imagine that your class has a dependency on a particular instance. If the dependency is null, you might have to throw an exception before you proceed. If you have a method that returns an object then you might have to do a null check before you proceed as well to avoid runtime exceptions. The goal of the NULL Object pattern is to enable you to avoid writing boiler plate code (checking for null values time and again) and instead return an empty object to the consumer. So, instead of returning a null, in a method when implementing the NULL Object pattern, all you have to do is return an empty object.

How do I implement the NULL Object pattern?

Let’s dig into some code now. In this section I will present a discussion on how the NULL Object pattern can be implemented.

In this implementation, the participant classes would include the following:

  1. An abstract class named BaseProduct that would serve as the base of the concrete implementations
  2. A concrete class called Product (this is the actual class)
  3. Another concrete class called NullProduct (this is the NullClass, i.e., a do-nothing class)

Consider the following abstract base class named BaseProduct. It contains the GetProductByProductCode method that returns an instance of the Product class if the product code is non empty. If the product code passed to it as a parameter is null or empty, it returns an instance of NullProduct class instead.

public abstract class BaseProduct

   {

       protected string ProductCode = string.Empty;

       public BaseProduct(string pCode)

       {

           ProductCode = pCode;

       }

       public BaseProduct GetProductByProductCode(string productCode)

       {

           if(IsProductAvailable(productCode))

               return new Product(productCode);

           return new NullProduct(string.Empty);

       }

       public abstract bool IsProductAvailable(string pCode);

   }

The NullProduct is shown below. The IsProductAvailable method is overridden in this class and it always returns a false value.

class NullProduct : BaseProduct

   {

       public NullProduct(string pCode) : base(pCode) { }

       public override bool IsProductAvailable(string pCode)

       {

           return false;

       }

   }

Note that both the Product and the NullProduct classes extends the BaseProduct class. Here’s how the Product class looks like.

class Product : BaseProduct

   {

       public Product(string pCode = "") : base(pCode) { }

       public override bool IsProductAvailable(string pCode)

       {

           if (string.IsNullOrEmpty(pCode))

               return false;

          return true;

       }

   }

The following code snippet illustrates how you can invoke the GetProductByProductCode method.

BaseProduct product = new Product();

product = product.GetProductByProductCode("P0001");

In this case, since the product code passed as an argument to the GetProductByProductCode method is valid, an instance of the Product class is returned. On the contrary, if you pass an empty string when calling the GetProductByProductCode method, an instance of NullProduct will be returned.

BaseProduct product = new Product();

product = product.GetProductByProductCode("");

Note that the IsProductAvailable method can be more complex in reality. The example shown here assumes it to be simple just for illustration purposes only. In lieu of an empty product code you may pass a product code which would be searched for in the database and if found, an instance of the Product class would be created and returned.

This article is published as part of the IDG Contributor Network. Want to Join?

Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here