SlideShare a Scribd company logo
1 of 10
Null Object Pattern 
Avoids null pointer exceptions. 
Replaces conditionals with polymorphism.
The Problem 
“Name” 
print $house->getTenant()->getName(); 
Error! 
Cannot call method on non-object.
The Wrong Solution 
$tenant = $house->getTenant(); 
if ($tenant) { 
print $tenant->getName(); 
} else { 
print “no tenant”; 
}
The Null Object Solution 
class House { 
private $tenant; 
function getTenant() { 
return $this.tenant 
?: NullTenant::getInstance(); 
} 
class NullTenant implements Tenant { 
function getName() { 
return “no tenant”; 
} 
print $house->getTenant()->getName();
Salient Points 
• Null Objects are Singletons 
• Null Objects keep no state. 
• Null Objects implement the same interface as the 
“real” objects. 
• Null Objects “do the right thing” when a null is 
expected.
Salient Points 
More generally the rule is: never return a null 
when your client is expecting an object, return 
a Null Object instead.
Real World 
class Node { 
private $left, $right; 
function copy() { 
$leftcopy = $this->left 
? $this->left->copy(); 
: null; 
$rightcopy = $this->right 
? $this->right->copy(); 
: null; 
return new self($leftcopy, 
$rightcopy); 
}
Real World 
class RealNode implements Node { 
private $left, $right; 
function copy() { 
return new self($left->copy(), 
$right->copy()); 
} 
class NullNode implements Node { 
function copy() { 
return $this; 
}
Real World 
• Objective C has the Null Object Pattern built-in! 
• Calling a method on null in Objective C results in 
null.
Real World 
• Fowler calls this the “Special Case Object” pattern. 
• We can have alternative null objects for different 
circumstances, i.e. 
• NoTennant 
• UnknownTennant

More Related Content

What's hot

Javascript: the important bits
Javascript: the important bitsJavascript: the important bits
Javascript: the important bits
Chris Saylor
 

What's hot (9)

Javascript: the important bits
Javascript: the important bitsJavascript: the important bits
Javascript: the important bits
 
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?What's up with Prototype and script.aculo.us?
What's up with Prototype and script.aculo.us?
 
10 tips for a reusable architecture
10 tips for a reusable architecture10 tips for a reusable architecture
10 tips for a reusable architecture
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 30 of 88The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 30 of 88
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Test doubles
Test doublesTest doubles
Test doubles
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 

Null object

  • 1. Null Object Pattern Avoids null pointer exceptions. Replaces conditionals with polymorphism.
  • 2. The Problem “Name” print $house->getTenant()->getName(); Error! Cannot call method on non-object.
  • 3. The Wrong Solution $tenant = $house->getTenant(); if ($tenant) { print $tenant->getName(); } else { print “no tenant”; }
  • 4. The Null Object Solution class House { private $tenant; function getTenant() { return $this.tenant ?: NullTenant::getInstance(); } class NullTenant implements Tenant { function getName() { return “no tenant”; } print $house->getTenant()->getName();
  • 5. Salient Points • Null Objects are Singletons • Null Objects keep no state. • Null Objects implement the same interface as the “real” objects. • Null Objects “do the right thing” when a null is expected.
  • 6. Salient Points More generally the rule is: never return a null when your client is expecting an object, return a Null Object instead.
  • 7. Real World class Node { private $left, $right; function copy() { $leftcopy = $this->left ? $this->left->copy(); : null; $rightcopy = $this->right ? $this->right->copy(); : null; return new self($leftcopy, $rightcopy); }
  • 8. Real World class RealNode implements Node { private $left, $right; function copy() { return new self($left->copy(), $right->copy()); } class NullNode implements Node { function copy() { return $this; }
  • 9. Real World • Objective C has the Null Object Pattern built-in! • Calling a method on null in Objective C results in null.
  • 10. Real World • Fowler calls this the “Special Case Object” pattern. • We can have alternative null objects for different circumstances, i.e. • NoTennant • UnknownTennant