How to create Null Object Pattern in Java

  • 07 December 2016
  • ADM

 

How to create Null Object Pattern in Java - images/logos/oopjava.jpg

 

In Null Object Pattern a null object replaces the check of NULL object instance. This is useful when you don't want to have null objects, but instead you need an object with default values.

Implementation

To do this will create a interface to define the objects and two classes:

  • one class will define the real objects
  • the second class will define the Null Object used in case a real object is not define.

Will pick the example with a simple model interface.

package com.admfactory.pattern;

public interface Model {

    public int getId();

    public String getName();
}

The implementation for the real object.

package com.admfactory.pattern;

public class User implements Model {

    private int id;

    private String name;

    public User(int id, String name) {
	this.id = id;
	this.name = name;
    }

    @Override
    public int getId() {
	return id;
    }

    @Override
    public String getName() {
	return name;
    }

    @Override
    public String toString() {
	return String.format("[id:%d,name:%s]", getId(), getName());
    }

}

The implementation for the Null Object.

package com.admfactory.pattern;

public class NullUser implements Model {

    @Override
    public int getId() {
	return 0;
    }

    @Override
    public String getName() {

	return "Null User";
    }
    
    @Override
    public String toString() {
	return String.format("[id:%d,name:%s]", getId(), getName());
    }

}

Also a factory class is needed to return the object if exists or instead to return a Null object.

To be more easier the factory class will create all the users and will store it in a local Hashtable. In real world this class will handle the database reading.

package com.admfactory.pattern;

import java.util.Hashtable;

public class UserFactory {

    private static Hashtable<String, Model> users = new Hashtable<String, Model>();

    static {
	Model u1 = new User(1, "John");
	users.put(String.valueOf(u1.getId()), u1);
	
	Model u2 = new User(2, "Jan");
	users.put(String.valueOf(u2.getId()), u2);
	
	Model u3 = new User(3, "Jose");
	users.put(String.valueOf(u3.getId()), u3);
    }

    public static Model getUser(int id) {
	String key = String.valueOf(id);
	Model user = users.get(key);
	if (user == null)
	    user = new NullUser();
	return user;
    }
}

Example

Simple example using the Null Design Pattern implementation.

package com.admfactory.pattern;

public class NullPatternExample {
    public static void main(String[] args) {
	Model u1 = UserFactory.getUser(1);
	Model u2 = UserFactory.getUser(2);
	Model u3 = UserFactory.getUser(3);
	Model u4 = UserFactory.getUser(4);
	Model u5 = UserFactory.getUser(5);
	
	System.out.println("Java Null Design Pattern Example");
	System.out.println();
	System.out.println(u1.toString());
	System.out.println(u2.toString());
	System.out.println(u3.toString());
	System.out.println(u4.toString());
	System.out.println(u5.toString());
	
    }
}

Output

Java Null Design Pattern Example

[id:1,name:John]
[id:2,name:Jan]
[id:3,name:Jose]
[id:0,name:Null User]
[id:0,name:Null User]

You can notice that the u4 and u5 object are not present in the hashtable and an instantiation of Null object is return for these.