<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>the fancy part of the web &#187; null</title>
	<atom:link href="http://blog.tolleiv.de/tag/null/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tolleiv.de</link>
	<description>is elsewhere - this is just about all sorts of web related work with a small factor of fanciness</description>
	<lastBuildDate>Sat, 21 Jan 2012 12:58:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Null-Object Pattern</title>
		<link>http://blog.tolleiv.de/2008/07/null-object-pattern/</link>
		<comments>http://blog.tolleiv.de/2008/07/null-object-pattern/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 17:58:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[behavioral]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/07/null-object-pattern/</guid>
		<description><![CDATA[Very often you create a object with a factory and before you really start using it, you check if your factory really created a object or returned NULL. Or maybe you have a method where a object is passed in and in this situation you'll have to do this check also.

Instead of typing the ...]]></description>
			<content:encoded><![CDATA[<p>Very often you create a object with a factory and before you really start using it, you check if your factory really created a object or returned NULL. Or maybe you have a method where a object is passed in and in this situation you&#8217;ll have to do this check also.</p>
<p>Instead of typing the &#8220;<span style="font-style: italic;">if(object == null)</span>&#8221; phrase again and again, you could use the <span style="font-weight: bold;">Null-Object pattern</span>, you&#8217;ll see that this can make some situations much clearer.</p>
<p>Basically Null-Object ensures that the client always receives a valid object for it&#8217;s interaction, so that there&#8217;s no need to do the check shown above again and again. This happens since your concrete Null-Object just shares the interface, or inherits from the same class as it&#8217;s effective counterpart, but it&#8217;s implementation just leaves out any effectiveness.<br />
<a href="http://bp3.blogger.com/_l5fIZzJyYfc/SGpiDZ9m-rI/AAAAAAAAACo/nVuiUHE4E90/s1600-h/nullobject_pattern.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" rel="lightbox[584]"><br />
<img id="BLOGGER_PHOTO_ID_5218090928910564018" style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp3.blogger.com/_l5fIZzJyYfc/SGpiDZ9m-rI/AAAAAAAAACo/nVuiUHE4E90/s400/nullobject_pattern.png" border="0" alt="" /></a> So a code-example could look like this:</p>
<p><!-- Code start --><br />
<span style="color: #007700;">class </span><span style="color: #0000bb;">CookieFactory </span><span style="color: #007700;">{<br />
public function </span><span style="color: #0000bb;">makeInstance</span><span style="color: #007700;">() {<br />
if(</span><span style="color: #0000bb;">date</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;l&#8217;</span><span style="color: #007700;">)==</span><span style="color: #dd0000;">&#8216;Monday&#8217;</span><span style="color: #007700;">) return new </span><span style="color: #0000bb;">NullCookie</span><span style="color: #007700;">();<br />
return new </span><span style="color: #0000bb;">RealCookie</span><span style="color: #007700;">();<br />
}<br />
}</span></p>
<p>interface <span style="color: #0000bb;">iCookie </span><span style="color: #007700;">{<br />
function </span><span style="color: #0000bb;">getCalories</span><span style="color: #007700;">();<br />
}</span></p>
<p>class <span style="color: #0000bb;">RealCookie </span><span style="color: #007700;">implements </span><span style="color: #0000bb;">iCookie </span><span style="color: #007700;">{<br />
protected </span><span style="color: #0000bb;">$calories</span><span style="color: #007700;">=</span><span style="color: #0000bb;">250</span><span style="color: #007700;">;<br />
public function </span><span style="color: #0000bb;">getCalories</span><span style="color: #007700;">() {<br />
return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">calories</span><span style="color: #007700;">;<br />
}<br />
}</span></p>
<p>class <span style="color: #0000bb;">NullCookie </span><span style="color: #007700;">implements </span><span style="color: #0000bb;">iCookie </span><span style="color: #007700;">{<br />
public function </span><span style="color: #0000bb;">getCalories</span><span style="color: #007700;">() {<br />
return </span><span style="color: #0000bb;">0</span><span style="color: #007700;">;<br />
}<br />
}<br />
</span><br />
<!-- Code end --></p>
<p>I think you can imagine what happens when you make use of the CookieFactory -<span style="font-style: italic;"> diet on monday <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </span><span> </span><span style="font-style: italic;"><br />
</span><br />
There&#8217;re also some disadvantages, your clients normally don&#8217;t have a chance to react that there&#8217;s something special </span><span>happening, also the clients must &#8220;share&#8221; the same expectation what &#8220;do nothing&#8221; means, </span></span>the number of required Null-Objects might be very large and unhandy and the Null-Object shares a very deep knowledge with the real one, so that it might be a large effort to create it if the real object is complex too.</p>
<p>I&#8217;m so glad that today is tuesday <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=584&amp;md5=632f5fcc5366e0bf0e098609646e3ed0" title="Flattr" target="_blank"><img src="http://blog.tolleiv.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.tolleiv.de/2008/07/null-object-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

