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:
- Needs to streamline the NULL handling in your application.
- The null object pattern can also be used to act as a stub for testing if a certain feature such as a database is not available for testing.
<Here are ALL other Java Design Patterns, explained in detail with examples>
Null Object Pattern by Example
Let us see this pattern using a simple Shop and Product example. The example contains:
- Abstract Class: The abstract class has to define the abstract primitive operations which are defined by the concrete implementations.
- Concrete Class: The Real Class is responsible for implementing the Abstract class, performing some real operations.
- Null Class: The Null class implements the abstract class, with empty logic.
- Client: The client receives the implementation of the abstract class and uses it
First, we will define a simple abstract class
1 2 3 4 5 6 7 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class ProductFactory { public static final String[] names = { "Shoes", "TShirt", "Jeans", "Bag", "Sack" }; public static Shop getProduct(String name) { for (int i = 0; i < names.length; i++) { if (names[i].equalsIgnoreCase(name)) { return new Product(name); } } return new NullProduct(); } } |
Running the Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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
1 2 3 4 5 |
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
<Here are ALL other Java Design Patterns, explained in detail with examples>