<?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; factory</title>
	<atom:link href="http://blog.tolleiv.de/tag/factory/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>
	</channel>
</rss>

