Introduce Null Object

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://elykogit.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 +
----
 +
=[http://elykogit.co.cc CLICK HERE]=
 +
----
 +
</div>
 
[[Category:Design Patterns]]
 
[[Category:Design Patterns]]
 
[[Category:Behavioural Patterns]]
 
[[Category:Behavioural Patterns]]
Line 25: Line 33:
  
 
* Create a subclass that acts as a null version of the class.  
 
* Create a subclass that acts as a null version of the class.  
* Create an isNull() method in both classes. For the superclass it should return "false", and "true" for the subclass.
+
* Create an isNull() method in both classes. For the superclass it should return &quot;false&quot;, and &quot;true&quot; for the subclass.
 
* Find all places that can give out a null value when asked for an object of the superclass and replace them to give a null object instead.
 
* Find all places that can give out a null value when asked for an object of the superclass and replace them to give a null object instead.
 
* Find all places that compare a variable of the superclass type with null and replace them with a call to isNull().
 
* Find all places that compare a variable of the superclass type with null and replace them with a call to isNull().

Revision as of 09:59, 24 November 2010


Summarised from Refactoring Martin Fowler 1999

Adds an object that defines use and behaviour but does nothing.

Use When

If you have repeated checks for a null reference, because one can't invoke anything on a null reference.

For example your code looks like:

if (customer == null)
  plan = BillingPlan.basic();
else 
  plan = customer.getPlan();

This 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.

Structure

Replace your check for a null reference with a null object

Null Object.jpg

  • Create a subclass that acts as a null version of the class.
  • Create an isNull() method in both classes. For the superclass it should return "false", and "true" for the subclass.
  • Find all places that can give out a null value when asked for an object of the superclass and replace them to give a null object instead.
  • Find all places that compare a variable of the superclass type with null and replace them with a call to isNull().


Consequences

This neglates the need to check for a null reference.

Another advantage of this approach over a working default implementation is that a null object is predictable and has no side effects: it does nothing.

Related Patterns

It can be regarded as a special case of the State pattern and the Strategy Pattern.

Personal tools