Null Object design pattern is also known as Stub and Active Nothing. It provides an object as a surrogate for the lack of an object of a given type. The Null Object provides intelligent do nothing behavior, hiding the details from its collaborators. In object-oriented languages such as Java references may be null and needs to be checked to ensure they are not null before invoking any methods.

Situations of using Null Object Pattern

Use the Null Object pattern when:

  • Some collaborator instances should do nothing
  • You want clients to ignore the difference between a collaborator that does something and one that does nothing – so the client does not have to explicitly check for null or some other special value
  • You want to be able to reuse the do-nothing behavior so that various clients that need this behavior will consistently work in the same way

Use a variable containing null or some other special value instead of the Null Object pattern when:

  • Very little code actually uses the variable directly
  • The code that does use the variable is well encapsulated – at least in one class
  • The code that uses the variable can easily decide how to handle the null case and will always handle it the same way

Solution

Null Object implements all the operations of the real object, but these operations do nothing.

Null Object Implementation using Java

If there are too Many classes, then eliminate one class by making NullObject a subclass of RealObject. If different clients expect do nothing behavior to mean different things, then use Adapter pattern to provide different do nothing behavior to Null Object. In some cases a message to NullObject should transform it to a real object. Use the proxy pattern transformation to a RealObject.

Immutable Linked List Example

A Linked list is either a head element and a tail which is a list or empty (i.e. Null). Thus is makes sense to model an abstract linked list with a class List which has two methods getTail and accept.

public abstract class List {
   public abstract List getTail();
   public abstract Object accept(ListVisitor visitor, Object param);
}

Now we will need a class for non-empty lists, we’ll call it NonNullList

public class NonNullList extends List {
   private Object head;
   private Object tail;

   /* Creates a list from a head and tail. Acts as "cons“*/

   public NonNullList(Object head, Object tail) {
      this.head = head;
      this.tail = tail;
   }
public Object getHead() {
      return head;
   }

   public List getTail() {
      return tail;
   }

   public Object accept(ListVisitor visitor, Object param) {
      return visitor.whenNonNullList(this, param);
   }
}

Null Object class:

public class NullList extends List {
   private static final NullList instance = new NullList();

   private NullList() { }

   public static NullList Singleton() {
      return instance;
   }

   public List getTail() {
      return this;
   }
   public Object accept(ListVisitor visitor, Object param) {
      return visitor.whenNullList(this, param);
   }
}

Visitor Interface

The entire system is completed by the visitor interface

public interface ListVisitor {
   public Object whenNonNullList(NonNullList host, Object param);

   public Object whenNullList(NullList host, Object param);
}

Notice that only the use of the Null Object pattern makes it safe to use a visitor. Otherwise a null pointer exception would occur when one tried to call accept on an empty list.

Consequences

Advantages

  • Uses polymorphic classes Simplifies client code
  • Encapsulates do nothing behavior
  • Makes do nothing behavior reusable

Disadvantages

  • Forces encapsulation
  • Makes it difficult to distribute or mix into the behavior of several collaborating objects
  • May cause class explosion
  • Forces uniformity
  • Different clients may have different idea of what “do nothing” means
  • Is non-mutable
  • NullObject objects can not transform themselves into a RealObject
Tagged with: Design PatternJAVAProgramming
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.