opencodez

Java Behavioral Design Patterns – Null Object Pattern

The Null Object Pattern is one of the behavioral design patterns, it is an object with clear neutral or null behavior. It is called as Null object because by default it does nothing and only it helps to avoid NULL Pointer Exceptions.

In Object oriented programming we have to take care of Null Pointer Exceptions which are due to the absence of an object. In this pattern, we make sure that an empty, not null object is returned for processing, e.g. an empty list.  The calling function may simply iterate the list as normal, effectively doing nothing.

You can opt for this design pattern if your application:

xHere are ALL other Java Design Patterns, explained in detail with examplesx

Null Object Pattern by Example

Let us see this pattern using a simple Shop and Product example. The example contains:

  1. Abstract Class: The abstract class has to define the abstract primitive operations which are defined by the concrete implementations.
  2. Concrete Class: The Real Class is responsible for implementing the Abstract class, performing some real operations.
  3. Null Class: The Null class implements the abstract class, with empty logic.
  4. Client: The client receives the implementation of the abstract class and uses it

First, we will define a simple abstract class

public abstract class Shop {
	
	public abstract void discountedProduct();

	public abstract boolean noDiscount();

}

After this let us add a concrete class, which provides some sample implementation

public class Product extends Shop {

	String name;

	public Product(String name) {
		this.name = name;
	}

	@Override
	public void discountedProduct() {
		System.out.println("10% Discount");
	}

	@Override
	public boolean noDiscount() {
		return false;
	}

}

As mentioned earlier, Null Objects are the ones which are meant to avoid Null Pointer Exceptions by providing No Logic or Bare Minimum Implementation

public class NullProduct extends Shop {

	@Override
	public void discountedProduct() {
		System.out.println("No Discount on this item!");
	}

	@Override
	public boolean noDiscount() {
		return true;
	}

}

We will add a simple factory class which will return us the Product object. It could be an actual product or a Null Object.

public class ProductFactory {
	
	public static final String[] names = { "Shoes", "TShirt", "Jeans", "Bag", "Sack" };

	public static Shop getProduct(String name) {

		for (int i = 0; i x names.length; i++) {
			if (names[i].equalsIgnoreCase(name)) {
				return new Product(name);
			}
		}
		return new NullProduct();
	}

}

Running the Example

public class NullPatternDemo {
	
	public static void main(String[] args) {
		
		Shop s1 = ProductFactory.getProduct("Tshirt");
		Shop s2 = ProductFactory.getProduct("Trouser");
		Shop s3 = ProductFactory.getProduct("Officebag");
		Shop s4 = ProductFactory.getProduct("Shoes");
		Shop s5 = ProductFactory.getProduct("Belt");

		s1.discountedProduct();
		s2.discountedProduct();
		s3.discountedProduct();
		s4.discountedProduct();
		s5.discountedProduct();

	}
}

Above is example running our Null Object Pattern example.

Output

10% Discount
No Discount on this item!
No Discount on this item!
10% Discount
No Discount on this item!

Conclusion

In this article, we understood Null Object Design Pattern with the help of a simple example.  This pattern saves many efforts which are generally wasted to check for null values. This pattern makes the client code very simple by avoiding unnecessary null checks.

The source code is available in our Github repository.

Download Code

x

xHere are ALL other Java Design Patterns, explained in detail with examplesx

x