<?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; creational</title>
	<atom:link href="http://blog.tolleiv.de/tag/creational/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>Query Object [PoEEA]</title>
		<link>http://blog.tolleiv.de/2008/06/query-object-poeea/</link>
		<comments>http://blog.tolleiv.de/2008/06/query-object-poeea/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 17:29:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[creational]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[specification]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/06/query-object-poeea/</guid>
		<description><![CDATA[Maybe you remember the Specification Pattern I explained some weeks ago. It enabled a easy and intuitive  searching within large object-collections. A drawback of my example was that I stored the objects in the memory. This can be really ineffective if you want a single object out of hundreds, because you have to ...]]></description>
			<content:encoded><![CDATA[<p>Maybe you remember the <a href="http://www.cookiepattern.com/2008/05/specification-ddd.html">Specification Pattern</a> I explained some weeks ago. It enabled a easy and intuitive  searching within large object-collections. A drawback of my example was that I stored the objects in the memory. This can be really ineffective if you want a single object out of hundreds, because you have to create all of them to see which one fits the specification.<br />
Normally you want to limit the number of objects and you also don&#8217;t store large datasets in memory. The idea of the<span style="font-style: italic;"> Query Object</span> pattern is that it enables a usage, </span><span style="font-family: trebuchet ms; font-size: 85%;">comparable to the specification pattern, for objects which are persisted in a database. The benefit is that it creates a query to exclude objects which won&#8217;t satisfy your needs and therefore you wont mess up the memory anymore.<br />
Once you have a query-object in place you should not get in touch with SQL anymore because it can encapsulate SQL completely, at least if you also have some kind of data mapping (coming soon), which is a great benefit for everyone who is not so familiar with SQL. (But don&#8217;t forget, regarding performance, SQL-optimization is a very important thing).</p>
<p>So what we need for the <span style="font-style: italic;">Query Object</span> in first place is a object, I&#8217;ll use the Cookie out of the <a href="http://www.cookiepattern.com/2008/05/specification-ddd.html">specification pattern post</a> again. Then we need criteria-objects which hold the information for a single criteria, (determined by &#8220;database-field&#8221;, &#8220;operator&#8221; and &#8220;value&#8221;) and we also need the <span style="font-style: italic;">Query Objects</span> itself to wrap up the SQL-querying and the object creation somehow.</p>
<p>A very simple example could look like this:</p>
<p><a name="code"></a><br />
<!-- Code start --><br />
<span style="color: #007700;"><br />
interface </span><span style="color: #0000bb;">Critera </span><span style="color: #007700;">{<br />
public function </span><span style="color: #0000bb;">getWhereClause</span><span style="color: #007700;">();<br />
}</span></p>
<p>class <span style="color: #0000bb;">CookieCriteria </span><span style="color: #007700;">implements </span><span style="color: #0000bb;">Critera </span><span style="color: #007700;">{</span></p>
<p>private <span style="color: #0000bb;">$operator</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">;</span></p>
<p>protected function <span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$operator</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">) {<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">operator</span><span style="color: #007700;">=</span><span style="color: #0000bb;">$operator</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">field</span><span style="color: #007700;">=</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">value</span><span style="color: #007700;">=</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">;<br />
}</span></p>
<p>public function <span style="color: #0000bb;">getWhereClause</span><span style="color: #007700;">() {<br />
return </span><span style="color: #0000bb;">implode</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8221; &#8220;</span><span style="color: #007700;">,array(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">field</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">operator</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">value</span><span style="color: #007700;">));<br />
}</span></p>
<p>public static function <span style="color: #0000bb;">matches</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">) {<br />
return new </span><span style="color: #0000bb;">CookieCriteria</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8220;LIKE&#8221;</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8216;&#8221;&#8216;</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">.</span><span style="color: #dd0000;">&#8216;&#8221;&#8216;</span><span style="color: #007700;">);<br />
}</span></p>
<p>public static function <span style="color: #0000bb;">greaterThan</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">) {<br />
return new </span><span style="color: #0000bb;">CookieCriteria</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8220;&gt;&#8221;</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$field</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$value</span><span style="color: #007700;">);<br />
}<br />
}</span></p>
<p>class <span style="color: #0000bb;">CookieFinder </span><span style="color: #007700;">{<br />
protected </span><span style="color: #0000bb;">$criterias</span><span style="color: #007700;">;</span></p>
<p>public function <span style="color: #0000bb;">addCriteria</span><span style="color: #007700;">(</span><span style="color: #0000bb;">Critera $criteria</span><span style="color: #007700;">) {<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">criterias</span><span style="color: #007700;">[] = </span><span style="color: #0000bb;">$criteria</span><span style="color: #007700;">;<br />
}</span></p>
<p>public function <span style="color: #0000bb;">generateSQL</span><span style="color: #007700;">() {<br />
</span><span style="color: #0000bb;">$sql </span><span style="color: #007700;">= </span><span style="color: #dd0000;">&#8220;SELECT * FROM cookies&#8221;</span><span style="color: #007700;">;</span></p>
<p>if(<span style="color: #0000bb;">sizeof</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">criterias</span><span style="color: #007700;">)) {<br />
</span><span style="color: #0000bb;">$where</span><span style="color: #007700;">=array();<br />
</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;">criterias</span><span style="color: #007700;">);<br />
while(list(,</span><span style="color: #0000bb;">$criteria</span><span style="color: #007700;">)=</span><span style="color: #0000bb;">each</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">criterias</span><span style="color: #007700;">)) {<br />
</span><span style="color: #0000bb;">$where</span><span style="color: #007700;">[] = </span><span style="color: #0000bb;">$criteria</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">getWhereClause</span><span style="color: #007700;">();<br />
}<br />
</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">.= </span><span style="color: #0000bb;">sizeof</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$where</span><span style="color: #007700;">)?</span><span style="color: #dd0000;">&#8221; WHERE &#8220;</span><span style="color: #007700;">.</span><span style="color: #0000bb;">implode</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8221; AND &#8220;</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$where</span><span style="color: #007700;">):</span><span style="color: #dd0000;">&#8220;&#8221;</span><span style="color: #007700;">;<br />
}<br />
return </span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">;<br />
}</span></p>
<p>public function <span style="color: #0000bb;">find</span><span style="color: #007700;">() {<br />
</span><span style="color: #0000bb;">$collection </span><span style="color: #007700;">= array();<br />
if(!</span><span style="color: #0000bb;">$result </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">generateSQL</span><span style="color: #007700;">())) {<br />
throw new </span><span style="color: #0000bb;">Exception</span><span style="color: #007700;">(</span><span style="color: #0000bb;">mysql_errno</span><span style="color: #007700;">());<br />
}<br />
while(</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_fetch_assoc</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$result</span><span style="color: #007700;">)) {<br />
</span><span style="color: #0000bb;">$collection</span><span style="color: #007700;">[] = new </span><span style="color: #0000bb;">Cookie</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">],</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'flavor'</span><span style="color: #007700;">],</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'size'</span><span style="color: #007700;">]);<br />
}<br />
return </span><span style="color: #0000bb;">$collection</span><span style="color: #007700;">;<br />
}<br />
}<br />
</span><br />
<!-- Code end --></p>
<p></span><br />
Possible client code could look like this:<br />
<span style="color: #0000bb;">$finder </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">CookieFinder</span><span style="color: #007700;">();<br />
</span><span style="color: #0000bb;">$finder</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addCriteria</span><span style="color: #007700;">(</span><span style="color: #0000bb;">CookieCriteria</span><span style="color: #007700;">::</span><span style="color: #0000bb;">matches</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8220;name&#8221;</span><span style="color: #007700;">,</span><span style="color: #dd0000;">&#8220;Granny%&#8221;</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$finder</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">addCriteria</span><span style="color: #007700;">(</span><span style="color: #0000bb;">CookieCriteria</span><span style="color: #007700;">::</span><span style="color: #0000bb;">greaterThan</span><span style="color: #007700;">(</span><span style="color: #dd0000;">&#8220;size&#8221;</span><span style="color: #007700;">,</span><span style="color: #0000bb;">100</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$cookies </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$finder</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">find</span><span style="color: #007700;">();<br />
</span><br />
We just pick up the <span style="font-style: italic;">Query Object</span>, add one or more criteria and ask it to create the objects which fit them.</p>
<p>So this example is not as powerful as the one I used for the <a href="http://www.cookiepattern.com/2008/05/specification-ddd.html">Specification pattern</a>, but it should be a easy task to create some kind of &#8220;nested criteria objects&#8221;.<br />
Query objects normally make use of data-mapping so that you can handle various classes, stored in different tables/databases, with a single and generic Query Object. This also enables to avoid SQL-Injection, since you&#8217;re able to validate the fields and values before you sent them to your database, also some kind of database abstraction would be possible.<br />
With the &#8220;<span style="font-style: italic;">Query Object by example</span>&#8220;, which requires to build up a single object which is used as blueprint for the required objects, exists another flavor of this pattern which is very handy to use and more descriptive.<br />
But no matter which flavor you prefer, Query Objects bring some real benefits when you&#8217;ve to handle complex datasets &#8211; for smaller projects the effort might be to much so be careful where you use it.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=581&amp;md5=9a105b0742d66c49d5d7d462d0adf126" 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/06/query-object-poeea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plugin [PoEAA]</title>
		<link>http://blog.tolleiv.de/2008/06/plugin-poeaa/</link>
		<comments>http://blog.tolleiv.de/2008/06/plugin-poeaa/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 01:11:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[creational]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/06/plugin-poeaa/</guid>
		<description><![CDATA[I'm sure you're pretty often in the situation that you have to switch something depending on the context you're currently in. For example most people change their eating habits before summer - I also do :P

Sometimes this is what you also want to have in your software. To achieve different behaviour you normally just ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you&#8217;re pretty often in the situation that you have to switch something depending on the context you&#8217;re currently in. For example most people change their eating habits before summer &#8211; I also do <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Sometimes this is what you also want to have in your software. To achieve different behaviour you normally just implement two different classes or aggregates and since they have the same &#8220;meaning&#8221; they normally share a interface (a.k.a <span style="font-style: italic;">Separated Interface</span>). But who decides which implementation fits into the current environment/context?</p>
<p>The easiest way is to have a <span style="font-style: italic;">Factory Method</span> with a small condition to decide this, but this method might grow very fast if you have various options. In this situation its also a matter of form to move this decision into some kind of configuration(-file) so that there&#8217;s only on file which differs in various environments.</p>
<p><a href="http://martinfowler.com/eaaCatalog/plugin.html">Martin Fowler</a> suggests to place some kind of mapping into the configuration-file, so that your <span style="font-style: italic;">Factory</span> knows which implementation to instantiate in the current context. The key for the mapping in this case is the name of the <span style="font-style: italic;">Separated Interface</span>.</p>
<p>As you see in the example code below, there are Cookie Tins which <span style="text-decoration: line-through;">create</span> contain Cookies and depending on the current month you want to use a more or less restrictive Cookie Tin. In May, June and July you restrict your application to create max. 5 cookies, the rest of the year you don&#8217;t care <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<pre name="code" class="php">
	// The Separated Interface
interface CookieTin {
	public function getCookieInstance();
}

class NormalTin implements CookieTin {
	public function getCookieInstance() {
		return new Cookie();
	}
}

class DietTin implements CookieTin {
	protected $i=0;
	public function getCookieInstance() {
		return ($this->i++ < 5)?new Cookie():new NullCookie();
	}
}

// The Factory which supports Plugin-Creation
class CookieTinFactory {
	public static function getPlugin( $class ) {
		try {
			return new $GLOBALS['Plugins'][$class]();
		} catch( Exception $e) {
			// maybe call some default implementation if the mapping is wrong
		}
	}
}
</pre>
<pre name="code" class="php">
class Cookie {
	public function printCalories() {
		echo ’200 ‘;
	}
}

class NullCookie extends Cookie{
	public function printCalories() { }
}
</pre>
<pre name="code" class="php">
// This is usually in a configuration-file, normally this should
// also sit in a XML structure….

// That’s what it would be in 300 days/year
$GLOBALS['Plugins']['CookieTin'] = “NormalTin”;
// That’s what we have to turn it to before summer
if(in_array(date(‘n’),array(5,6,7))) {
	$GLOBALS['Plugins']['CookieTin'] = “DietTin”;
}

$tin = CookieTinFactory::getPlugin(‘CookieTin’);
$i=10;
while($i–) {
	$cookie = $tin->getCookieInstance();
	$cookie->printCalories();
}
</pre>
<p>A very common use-case for this is the changed behaviour in a testing-environment compared to the production-environment.<br />
There's something else in the example beside the <span style="font-style: italic;">Plugin Pattern</span> - I also made use of the <span style="font-style: italic;">Special Case (the NullCookie)</span> pattern  which I'm going to show in a future post....</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=572&amp;md5=cc38f62a87757a4302f79537af4c9652" 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/06/plugin-poeaa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prototype [GoF]</title>
		<link>http://blog.tolleiv.de/2008/05/prototype-gof/</link>
		<comments>http://blog.tolleiv.de/2008/05/prototype-gof/#comments</comments>
		<pubDate>Tue, 27 May 2008 23:17:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[creational]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/05/prototype-gof/</guid>
		<description><![CDATA[Imagine a cookie-oven which produces tasty cookies with chocolate crumbles. How do you ensure that the 1000th cookie still has the same taste as the first?
You might think that this is an easy task - just write down the recipe and follow the described steps...you know the result in real life - the 1000th ...]]></description>
			<content:encoded><![CDATA[<p>Imagine a cookie-oven which produces tasty cookies with chocolate crumbles. How do you ensure that the 1000th cookie still has the same taste as the first?<br />
You might think that this is an easy task &#8211; just write down the recipe and follow the described steps&#8230;you know the result in real life &#8211; the 1000th cookie normally tasts like the 1st but you always had the &#8220;overhead&#8221; to read the recipe and go through the steps again and again.<br />
In OOP it&#8217;s much easier to follow the recipe just instantiate a new Object and  there you go&#8230; no matter if it&#8217;s the 1st or the 1000th &#8211; it&#8217;ll always <span style="text-decoration: line-through;">taste</span> look similar.<br />
But the &#8220;recipe-overhead&#8221; is still there in a way and especially when you have larger objects whose construction is time-consuming you might want to somehow get rid of it. And that&#8217;s where a <span style="font-style: italic;">Prototype</span> can help you out &#8211; you just create the first <span style="text-decoration: line-through;">Cookie</span> Object and then you use the handy magic method <span style="font-weight: bold;">__clone</span> to create new objects.<br />
Instead of just using __clone the pattern suggests a class (some kind of a factory-class) so that you can also encapsulate the creation of the objects (and also possible adjustments you might want to make after the creation/clone).</p>
<p>So the example just shows a cookie-machine which makes use of the prototype-pattern to create new cookies (depending on the cookie you throw in before)&#8230; yummy</p>
<div style="text-align: center;"><a href="http://bp0.blogger.com/_l5fIZzJyYfc/SDrXY97tyhI/AAAAAAAAABg/1sAhpbVe2kI/s1600-h/prototype_pattern.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" rel="lightbox[568]"><img id="BLOGGER_PHOTO_ID_5204709143321889298" style="cursor: pointer;" src="http://bp0.blogger.com/_l5fIZzJyYfc/SDrXY97tyhI/AAAAAAAAABg/1sAhpbVe2kI/s400/prototype_pattern.png" border="0" alt="" /></a></div>
<p><a name="code1"></a><br />
<!-- Code start --></p>
<p><span style="color: #007700;">abstract class </span><span style="color: #0000bb;">Cookie </span><span style="color: #007700;">{<br />
function </span><span style="color: #0000bb;">__clone</span><span style="color: #007700;">() {    }<br />
abstract public function </span><span style="color: #0000bb;">printFlavor</span><span style="color: #007700;">();<br />
}</span></p>
<p>class <span style="color: #0000bb;">CoconutCookie </span><span style="color: #007700;">extends </span><span style="color: #0000bb;">Cookie </span><span style="color: #007700;">{<br />
public function </span><span style="color: #0000bb;">printFlavor</span><span style="color: #007700;">() {<br />
echo </span><span style="color: #dd0000;">&#8216;Coconut Flavor&lt;br/&gt;&#8217;</span><span style="color: #007700;">;<br />
}<br />
}<br />
class </span><span style="color: #0000bb;">ChocolateCookie </span><span style="color: #007700;">extends </span><span style="color: #0000bb;">Cookie </span><span style="color: #007700;">{<br />
public function </span><span style="color: #0000bb;">printFlavor</span><span style="color: #007700;">() {<br />
echo </span><span style="color: #dd0000;">&#8216;Chocolate Flavor&lt;br/&gt;&#8217;</span><span style="color: #007700;">;<br />
}<br />
}</span></p>
<p>class <span style="color: #0000bb;">CookieMachine </span><span style="color: #007700;">{<br />
protected </span><span style="color: #0000bb;">$cookie</span><span style="color: #007700;">;<br />
public function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">Cookie $cookie</span><span style="color: #007700;">) {<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">cookie </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$cookie</span><span style="color: #007700;">;<br />
}<br />
public function </span><span style="color: #0000bb;">makeCookie</span><span style="color: #007700;">() {<br />
return clone </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">cookie</span><span style="color: #007700;">;<br />
}<br />
}</span><br />
<!-- Code end --></p>
<p>The client-code can look like this:</p>
<p><a name="code2"></a><br />
<!-- Code start --><br />
<span style="color: #0000bb;">$coconutCookie </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">CoconutCookie</span><span style="color: #007700;">();<br />
</span><span style="color: #0000bb;">$coconutCookieMachine </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">CookieMachine</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$coconutCookie</span><span style="color: #007700;">);</span></p>
<p><span style="color: #0000bb;">$chocolateCookie </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">ChocolateCookie</span><span style="color: #007700;">();<br />
</span><span style="color: #0000bb;">$chocolateCookieMachine </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">CookieMachine</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$chocolateCookie</span><span style="color: #007700;">);</span></p>
<p><span style="color: #ff8000;">//while(true) {<br />
</span><span style="color: #007700;">for(</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">=</span><span style="color: #0000bb;">0</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">&lt;</span><span style="color: #0000bb;">5</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">++) {<br />
</span><span style="color: #0000bb;">$coconutCookieMachine</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">makeCookie</span><span style="color: #007700;">()-&gt;</span><span style="color: #0000bb;">printFlavor</span><span style="color: #007700;">();<br />
</span><span style="color: #0000bb;">$chocolateCookieMachine</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">makeCookie</span><span style="color: #007700;">()-&gt;</span><span style="color: #0000bb;">printFlavor</span><span style="color: #007700;">();<br />
}<br />
</span><br />
<!-- Code end --></p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=568&amp;md5=86dd451191fdc891dbe8ee7d61c070ad" 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/prototype-gof/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singelton [GoF]</title>
		<link>http://blog.tolleiv.de/2008/05/singelton-gof/</link>
		<comments>http://blog.tolleiv.de/2008/05/singelton-gof/#comments</comments>
		<pubDate>Fri, 23 May 2008 21:40:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[anti-pattern]]></category>
		<category><![CDATA[creational]]></category>
		<category><![CDATA[registry]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/05/singelton-gof/</guid>
		<description><![CDATA[Tis pattern is a very well know and a often discussed one.

Explaining it the tasty way: let's say you have exactly one well know place where you store all your cookies and whenever you need one you can easy point to that place. Normally that's really comfortable because whatever you do with it, you ...]]></description>
			<content:encoded><![CDATA[<p>Tis pattern is a very well know and a often discussed one.</p>
<p>Explaining it the tasty way: let&#8217;s say you have exactly one well know place where you store all your cookies and whenever you need one you can easy point to that place. Normally that&#8217;s really comfortable because whatever you do with it, you don&#8217;t have to search some place where you can find a cookie, you just go to that single &#8220;cookie tin&#8221; (or wherever you store cookies) and put a new cookie into it <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In modern OOP the singleton is not very popular, Eric Evans simple calls it anti-pattern. It makes it much harder to test your software and normally the user of the class, not the class itself, should know how many instances are needed.</p>
<p>But nevertheless for small applications it&#8217;s very comfortable to use it in some situations and as long as you&#8217;re also familiar with the drawbacks I think it&#8217;s ok to consider using it&#8230;.</p>
<p>So the example shows a cookie tin and how to use it and by the way it also implements some kind of lowlevel registry by using the <a href="http://www.php.net/manual/en/language.oop5.magic.php">PHP5 magic methods</a>, but that&#8217;s another story <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<pre name="code" class="php">
/**
* CookieTin a.k.a Singleton object
* we want to force our application
* to hold exactly one CookieTin object
*/
class CookieTin {
	private $_cookies=array();

	/* generic singleton part start*/

	private static $_instance=NULL;
	// prevent instatiation from the outside
	private function __construct() { }

	// prevent cloning
	private function __clone() {}

	public static function getInstance() {
		if(!self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	/* generic singleton part end */

	/* registry part start */
	public static function __set($key,Cookie $value) {
		echo “call setter\n”;
		self::$_instance->_cookies[$key] = $value;
	}

	public static function __get($key) {
		echo “call getter\n”;
		return self::$_instance->_cookies[$key];
	}

	public static function __isset($key) {
		echo “call isset\n”;
		return isset(self::$_instance->_cookies[$key]);
	}

	public static function __unset($key) {
		echo “call unset\n”;
		unset(self::$_instance->_cookies[$key]);
	}
/* registry part end */
}
/**
* Dummy Cookie
*/
class Cookie {
	public $name,$flavor;
}
</pre>
<pre name="code" class="php">
$tin = CookieTin::getInstance();
if(!isset($tin->granniescookie)) {
	$tin->granniescookie = new Cookie();
	$tin->granniescookie->name=“Grannies chocolate wonder”;
	$tin->granniescookie->flavor=“chocolate”;
}

CookieTin::getInstance()->granniescookie->flavor = “chocolte chips”;

var_dump($tin);
unset(CookieTin::getInstance()->granniescookie);
var_dump($tin);
</pre>
<p>The really important things happen in the last lines where the CookieTin is accessed statically and this change also (as you will see yourself) is reflected in the local instance if the $tin.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=566&amp;md5=315a98b6733133901013f5ee8227faed" 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/singelton-gof/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

