<?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; behavioral</title>
	<atom:link href="http://blog.tolleiv.de/tag/behavioral/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>Iterator [GoF]</title>
		<link>http://blog.tolleiv.de/2008/07/iterator-gof/</link>
		<comments>http://blog.tolleiv.de/2008/07/iterator-gof/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 17:30:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[behavioral]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/07/iterator-gof/</guid>
		<description><![CDATA[Lot's of people like to write down things into lists, so that they can go through that list later and check whether everything was fine. Normally every recipe has a list ~ there's always a list of ingredients at the beginning of the recipe.
This short example shows how such a list can be processed ...]]></description>
			<content:encoded><![CDATA[<p>Lot&#8217;s of people like to write down things into lists, so that they can go through that list later and check whether everything was fine. Normally every recipe has a list ~ there&#8217;s always a list of ingredients at the beginning of the recipe.<br />
This short example shows how such a list can be processed in PHP. So why would you want to have something else than a array to hold your objects? &#8211; My example still uses a array to hold the objects (uni- or bidirectional lists would also be possible) but it adds a kind of a facade to the array so that the common managements-tasks are handled within the List-Object. Everything you need for this example is present in PHP since version 5.0. The basic steps you need to do is to provide a <span style="font-style: italic;">&#8220;Object&#8221;</span> and an <span style="font-style: italic;">&#8220;ObjectList&#8221; </span>which implements the native <a href="http://www.php.net/manual/en/language.oop5.iterations.php"><span style="font-style: italic;">&#8220;Iterator&#8221;</span></a> interface and then you&#8217;re able to have very handy lists <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<a onclick="document.getElementById('codeblockIt1').style.display=((document.getElementById('codeblockIt1').style.display=='block')?'none':'block');" href="#codeIt1"></a></p>
<div id="codeblockIt1" class="code">
<p><span style="color: #007700;">class </span><span style="color: #0000bb;">Incredient </span><span style="color: #007700;">{</span></p>
<p>public <span style="color: #0000bb;">$name</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$amount</span><span style="color: #007700;">;<br />
public function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$amount</span><span style="color: #007700;">) {<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">name </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$name</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">amount </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$amount</span><span style="color: #007700;">;<br />
}<br />
}</span></p>
<p>class <span style="color: #0000bb;">Recipe </span><span style="color: #007700;">implements </span><span style="color: #0000bb;">Iterator </span><span style="color: #007700;">{<br />
public </span><span style="color: #0000bb;">$title</span><span style="color: #007700;">;<br />
private </span><span style="color: #0000bb;">$ingredients</span><span style="color: #007700;">;</span></p>
<p>public function <span style="color: #0000bb;">__construct </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$title</span><span style="color: #007700;">) {<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">title </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$title</span><span style="color: #007700;">;<br />
}</span></p>
<p>public function <span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(</span><span style="color: #0000bb;">Incredient $in</span><span style="color: #007700;">) {<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">ingredients</span><span style="color: #007700;">[] = </span><span style="color: #0000bb;">$in</span><span style="color: #007700;">;<br />
}</span></p>
<p>public function <span style="color: #0000bb;">current </span><span style="color: #007700;">()  {   return </span><span style="color: #0000bb;">current </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">ingredients</span><span style="color: #007700;">);    }<br />
public function </span><span style="color: #0000bb;">key </span><span style="color: #007700;">()      {   return </span><span style="color: #0000bb;">key</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">ingredients</span><span style="color: #007700;">);         }<br />
public function </span><span style="color: #0000bb;">valid </span><span style="color: #007700;">()    {   return </span><span style="color: #0000bb;">current </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">ingredients</span><span style="color: #007700;">);    }<br />
public function </span><span style="color: #0000bb;">rewind </span><span style="color: #007700;">()   {   return </span><span style="color: #0000bb;">reset </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">ingredients</span><span style="color: #007700;">);      }<br />
public function </span><span style="color: #0000bb;">next </span><span style="color: #007700;">()     {   return </span><span style="color: #0000bb;">next </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">ingredients</span><span style="color: #007700;">);       }<br />
}</span></p>
<p><!-- Code end --></p>
</div>
<div id="codeblockIt2" class="code" style="display: block;">
<p><span style="color: #0000bb;">$cookieRecipe </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">Recipe</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8220;Chocolate Cookie&#8221;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Flour&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;2.5 cups&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Baking soda&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 teaspoon&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Salt&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;0.5 teaspoon&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Butter&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 cup&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Sugar&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 cup&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Brown Sugar&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;0.5 cup&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Vanilla extract&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 teaspoon&#8217;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Egg&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1-2&#8242;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookieRecipe</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addIncredient</span><span style="color: #007700;">(new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Chocolate chips&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;2 cups&#8217;</span><span style="color: #007700;">));</span></p>
<p><span style="color: #ff8000;">// process recipe:<br />
</span><span style="color: #007700;">foreach(</span><span style="color: #0000bb;">$cookieRecipe </span><span style="color: #007700;">as </span><span style="color: #0000bb;">$inc</span><span style="color: #007700;">) {<br />
echo </span><span style="color: #0000bb;">$inc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">name</span><span style="color: #007700;">.</span><span style="color: #dd0000;">&#8220; =&gt; &#8221;</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$inc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">amount</span><span style="color: #007700;">.</span><span style="color: #dd0000;">&#8220;&lt;br/&gt;&#8221;</span><span style="color: #007700;">;<br />
}</span></p>
</div>
<p>As you see it&#8217;s pretty easy to have lists of objects in PHP. You might also think that always creating to some list-object over and over again is very odd and you&#8217;re right. For the most common tasks like <a href="http://www.php.net/manual/en/class.arrayiterator.php">iterating through arrays</a>, <a href="http://www.php.net/manual/en/class.directoryiterator.php">directory-lists</a> and a few more task you can use objects which are shipped with the Standard PHP Library ,which is also part of PHP since version 5 and mandatory in 5.3. So the example shown above could also look like this:</p>
<div id="codeblockIt3" class="code" style="display: block;">
<p><span style="color: #0000bb;">$recipe </span><span style="color: #007700;">= array();<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Flour&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;2.5 cups&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Baking soda&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 teaspoon&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Salt&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;0.5 teaspoon&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Butter&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 cup&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Sugar&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 cup&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Brown Sugar&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;0.5 cup&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Vanilla extract&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1 teaspoon&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Egg&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;1-2&#8242;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Incredient</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8216;Chocolate chips&#8217;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8217;2 cups&#8217;</span><span style="color: #007700;">);</span></p>
<p><span style="color: #0000bb;">$recipeIncObj </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">ArrayObject</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$recipe</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$ingredientsIt </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$recipeIncObj</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">getIterator</span><span style="color: #007700;">(); </span></p>
<p>foreach(<span style="color: #0000bb;">$ingredientsIt </span><span style="color: #007700;">as </span><span style="color: #0000bb;">$inc</span><span style="color: #007700;">) {<br />
echo </span><span style="color: #0000bb;">$inc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">name</span><span style="color: #007700;">.</span><span style="color: #dd0000;">&#8220; =&gt; &#8221;</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$inc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">amount</span><span style="color: #007700;">.</span><span style="color: #dd0000;">&#8220;&lt;br/&gt;&#8221;</span><span style="color: #007700;">;<br />
} </span></p>
</div>
<p>As I said at the beginning, there are lots of situations where you might want to have a list for something and if you store that list in PHP the Iterator-pattern can keep your code clean and tasty <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=585&amp;md5=1dde71e3068f7f29863578a1a14f9ca9" 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/iterator-gof/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Value Object [DDD]</title>
		<link>http://blog.tolleiv.de/2008/05/value-object-ddd/</link>
		<comments>http://blog.tolleiv.de/2008/05/value-object-ddd/#comments</comments>
		<pubDate>Fri, 30 May 2008 04:39:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[behavioral]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[value object]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/05/value-object-ddd/</guid>
		<description><![CDATA[This metaphor does not fit completely, and the "Money-Example" is omnipresent, but I really like the pattern and so that's the story:

Let's say you have your grannies recipe for a chocolate cookie and you want to keep this in mind, but also you like to somehow experiment with some new or different ingredients. Normally ...]]></description>
			<content:encoded><![CDATA[<p><span style="font-style: italic;">This metaphor does not fit completely, and the &#8220;Money-Example&#8221; is omnipresent, but I really like the pattern and so that&#8217;s the story:</span></p>
<p>Let&#8217;s say you have your grannies recipe for a chocolate cookie and you want to keep this in mind, but also you like to somehow experiment with some new or different ingredients. Normally you just keep in mind what you changed and later you write down the new recipe. But from time to time you might want to compare the two recipes or maybe you want to make cookies with the new and the changed recipes. In this situation it&#8217;s really good to have both written down on paper. <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In the world of OOP you could think of a solution using the <a href="http://cookiepattern.blogspot.com/search/label/memento?max-results=100">Memento-Pattern</a> but this doesn&#8217;t really fit the situation and it&#8217;s also some kind of overhead. That&#8217;s the reason why the <span style="font-weight: bold;">Value Object</span> is the pattern of my choice.</p>
<p>So let&#8217;s look at the recipes again &#8211; we want to add and remove ingredients without modifying the original recipe and we want to compare the resulting recipes. We don&#8217;t need to track a special identity of our recipes since we&#8217;re only &#8220;collecting&#8221; ingredients.</p>
<p>The idea of the Value Object is that every every method which somehow transforms the state of a object always returns a new object and the old object stays untouched</span><span style="font-family: trebuchet ms; font-size: 85%;">. Whenever needed you should also implement a method to compare objects, I must concede that the common &#8220;Money Example&#8221; shows much better when that&#8217;s needed &#8230;</p>
<p>Another edge of this pattern in PHP is that you can use method-chaining to perform multiple actions within a single line of code. So just have a look at the example you will like it&#8217;s taste <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre name="code" class="php">
class Recipe {
	protected $ingredients;

	public function __construct($ingred=”) {
		$this->ingredients = implode(‘,’,array_unique(explode(‘,’,$ingred)));
	}

	public function addIncredient($ingred=”) {
		return new Recipe($this->ingredients.‘,’.$ingred);
	}

	public function removeIncredien($ingred=”) {
		return new Recipe(preg_replace(‘/,{0,1}’.$ingred.‘,{0,1}/’,”,$this->ingredients.‘,’));
	}

	public function printIncredients() {
		echo str_replace(‘,’,‘<br/>’,$this->ingredients);
	}

	public function equals(Recipe $recipe) {
		return (strcmp($this->ingredients,$recipe->ingredients)===0)?true:false;
	}
}

$granniesRecipe = new Recipe(‘flour,baking soda,sugar,salt,butter,vanilla,chocolate’);
$aNewRecipe = $granniesRecipe->addIncredient(‘lemon’);
$coconutRecipe = $granniesRecipe->removeIncredien(‘chocolate’)->addIncredient(‘coconut’);

// check if that worked:
echo ‘<br/><strong> Grannies Cookie Recipe – Incredients are:</strong><br/>’;
$granniesRecipe->printIncredients();
echo ‘<br/><strong> A new Cookie Recipe could look like:</strong><br/>’;
$aNewRecipe->printIncredients();
echo ‘<br/><strong> A coconut cookie would look like:</strong><br/>’;
$coconutRecipe->printIncredients();
</pre>
<p><span style="font-style: italic;">The implementation is a bit odd, since it only collects the names of the ingredients but not the amount, but including the amounts of incredients would not change the concept  and that&#8217;s why I left it out. I hope you got a idea how the pattern works &#8211; the main thing is that there&#8217;s no identity and that new objects are instantiated as soon as the state of the old one would change.</span></p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=569&amp;md5=d6f9f358f611a968ce44ad9ff5933e80" 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/05/value-object-ddd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memento [GoF]</title>
		<link>http://blog.tolleiv.de/2008/05/memento-gof/</link>
		<comments>http://blog.tolleiv.de/2008/05/memento-gof/#comments</comments>
		<pubDate>Tue, 13 May 2008 07:59:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[behavioral]]></category>
		<category><![CDATA[memento]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/05/memento-gof/</guid>
		<description><![CDATA[Let's say you got a cookie from a repository your granny and you're not sure if you like the new taste. Wouldn't it be cool if you could just try it and revoke the operation first bite if you don't like it?

In the world of OOP thats a easy job which can be handled ...]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you got a cookie from </span><span style="font-family: trebuchet ms; font-size: 85%;"><span style="text-decoration: line-through;">a repository</span></span><span style="font-family: trebuchet ms; font-size: 85%;"> your granny and you&#8217;re not sure if you like the new taste. Wouldn&#8217;t it be cool if you could just try it and revoke the <span style="text-decoration: line-through;">operation</span> first bite if you don&#8217;t like it?</p>
<p>In the world of OOP thats a easy job which can be handled by the so called memento pattern &#8211; you just save the inner state of the object  within a memento object and revoke you actions whenever you like&#8230;</p>
<p>As UML the example looks like this:<br />
<a href="http://bp2.blogger.com/_l5fIZzJyYfc/SClV6dPibdI/AAAAAAAAABA/bFyT-BEsqrU/s1600-h/memento-pattern.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" rel="lightbox[559]"><img id="BLOGGER_PHOTO_ID_5199781707546521042" style="cursor: pointer;" src="http://bp2.blogger.com/_l5fIZzJyYfc/SClV6dPibdI/AAAAAAAAABA/bFyT-BEsqrU/s400/memento-pattern.png" border="0" alt="" /></a><br />
<a onclick="document.getElementById('codeblockMemento').style.display=((document.getElementById('codeblockMemento').style.display=='block')?'none':'block');" href="#codeMemento">Show PHP Source Code</a></p>
<pre name="code" class="php">
class CookieMemento {
    protected $flavor,$size;
    public function __construct($flavor,$size=100) {
        $this->flavor=$flavor;
        $this->size=$size;
    }
    public function getMemento() {
        return new CookieMemento($this->flavor,$this->size);
    }
    public function setMemento(CookieMemento $memento) {
        $this->flavor=$memento->flavor;
        $this->size=$memento->size;
    }
}

class Cookie extends CookieMemento {

    public function eat($reduceValue) {
        $this->size-=($reduceValue>$this->size)?$this->size:$reduceValue;
    }    

    public function printStatus() {
        echo ‘This cookie is a ’.$this->flavor.‘ cookie which has a size of ’.$this->size.‘.<br/>’;
    }
}

class Client {
    public function run() {
        $theCookie = new Cookie(‘chocolate’,100);
        $theMemento = $theCookie->getMemento();
        echo $theCookie->printStatus();
        $theCookie->eat(50);
        echo $theCookie->printStatus();
        $theCookie->eat(50);
        echo $theCookie->printStatus();
        echo ‘Hm I\’d like to eat it again …. *grin*<br/>’;
        $theCookie->setMemento($theMemento);
        echo $theCookie->printStatus();
        echo ‘ *biggrin* ’;
    }
}

$cookieMonster = new Client();
$cookieMonster->run();
</pre>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=559&amp;md5=9baecebbe03a2d7a7f640ff56a537219" 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/05/memento-gof/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

