SDSU CS 635 Advanced Object-Oriented Design & Programming
Spring Semester, 2002
Assignment 1 Comments & Null Object
Previous    Lecture Notes Index    Next    
© 2002, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 05-Feb-02

Contents of Doc 3, Assignment 1 Comments & Null Object


References
Object-Oriented Design Heuristics , Riel, Addison-Wesley, 1996,

Refactoring: Improving the Design of Existing Code, Fowler, 1999, pp. 260-266

“Null Object”, Woolf, in Pattern Languages of Program Design 3, Edited by Martin, Riehle, Buschmmann, Addison-Wesley, 1998, pp. 5-18

Doc 3, Assignment 1 Comments & Null Object Slide # 17

NullObject


Structure




NullObject implements all the operations of the real object,

These operations do nothing or the correct thing for nothing


Doc 3, Assignment 1 Comments & Null Object Slide # 18

Binary Search Tree Example

Without Null Object

public class BinaryNode {
   Node left = new NullNode();
   Node right = new NullNode();
   int key;
   
   public boolean includes( int value ) {
      if (key == value)
         return true;
      else if ((value < key) & left == null) )
         return false;
      else if (value < key)
         return left.includes( value );
      else if (right == null)
         return false;
      else
         return right.includes(value);
   }
etc.
}


Doc 3, Assignment 1 Comments & Null Object Slide # 19
Binary Search Tree ExampleClass Structure


Object Structure



Doc 3, Assignment 1 Comments & Null Object Slide # 20
Searching for a Key

public class BinaryNode extends Node {
   Node left = new NullNode();
   Node right = new NullNode();
   int key;
   
   public boolean includes( int value ) {
      if (key == value)
         return true;
      else if (value < key )
         return left.includes( value );
      else
         return right.includes(value);
   }
etc.
}
   
public class NullNode extends Node {
   public boolean includes( int value ) {
      return false;
   }
etc.
}


Doc 3, Assignment 1 Comments & Null Object Slide # 21
Comments on Example


No need check if left, right are null


Use singleton pattern for the one instance
Forces indicate that one may not want to use the Null Object pattern
However, familiarity with trees makes it easy to explain the pattern

Requires reference to parent or
Use proxy


Doc 3, Assignment 1 Comments & Null Object Slide # 22

Refactoring

Introduce Null Object[2]

You have repeated checks for a null value

Replace the null value with a null object

Example

customer isNil
   ifTrue: [plan := BillingPlan basic]
   ifFalse: [plan := customer plan]

becomes:


   NullCustomer>>plan
      ^BillingPlan basic


Now the code is:

plan := customer plan



Doc 3, Assignment 1 Comments & Null Object Slide # 23

Applicability


Use the Null Object pattern when:



Client does not have to explicitly check for null or some other special value


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





Doc 3, Assignment 1 Comments & Null Object Slide # 24

Consequences

Advantages






Disadvantages

Makes it difficult to distribute or mix into the behavior of several collaborating objects


Different clients may have different idea of what “do nothing” means

NullObject objects can not transform themselves into a RealObject


Doc 3, Assignment 1 Comments & Null Object Slide # 25

Implementation


Too Many classes

Eliminate one class by making NullObject a subclass of RealObject

Multiple Do-nothing meanings
If different clients expect do nothing to mean different things use Adapter pattern to provide different do-nothing behavior to NullObject

Transformation to RealObject

In some cases a message to NullObject should transform it to a real object

Use the proxy pattern



Doc 3, Assignment 1 Comments & Null Object Slide # 26
[1] See Riel
[2] Refactoring Text, pp. 260-266

Copyright ©, All rights reserved.
2002 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 05-Feb-02    Next