• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null Pattern

 
Ranch Hand
Posts: 686
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody guide me what is null pattern?
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good article found while googling around.
http://www.cs.oberlin.edu/~jwalker/nullObjPattern/.

[Edited]
Hey, it became a poetry.

---
A good article found
while googling around
---
.
[ December 30, 2005: Message edited by: Adeel Ansari ]
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have seen lot of code like the following piece


If there are multiple calls to the getProduct() method, the null check has to be done in all the places. The Null Object Pattern comes for the rescue here.

The idea is to make the getProduct() method return a dummy instance (of Product type), which provide some default behaviour.



So, the calling client will get an instance of NullProduct instead of null. No need to do any null check.

A better way to implement the pattern is to declare the NullProduct class inside the Product interface, and use the same instance everywhere.

Hope that helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic