<?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; prototype</title>
	<atom:link href="http://blog.tolleiv.de/tag/prototype/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>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>Decorator [GoF]</title>
		<link>http://blog.tolleiv.de/2008/05/decorator-gof/</link>
		<comments>http://blog.tolleiv.de/2008/05/decorator-gof/#comments</comments>
		<pubDate>Mon, 12 May 2008 22:59:00 +0000</pubDate>
		<dc:creator>tolleiv</dc:creator>
				<category><![CDATA[pattern]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[structural]]></category>

		<guid isPermaLink="false">http://blog.tolleiv.de/2008/05/decorator-gof/</guid>
		<description><![CDATA[As the first pattern I'd like to introduce the decorator pattern - it's one of the - structural patterns. It enables to extend the functionality of a existing method by wrapping a so called decorator-object.

So maybe you already know the situation ;) , your granny  is going to bake cookies and you think ...]]></description>
			<content:encoded><![CDATA[<p>As the first pattern I&#8217;d like to introduce the decorator pattern &#8211; it&#8217;s one of the [GoF]- structural patterns. It enables to extend the functionality of a existing method by wrapping a so called decorator-object.</p>
<p>So maybe you already know the situation <img src='http://blog.tolleiv.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  , your granny  is going to bake cookies and you think of how they gonna taste &#8211; so cookie is our <span style="font-style: italic;">main-object</span> and the different additional spices and other options which refine the taste of the cookies are the <span style="font-style: italic;">decoration</span> for it. The cookies, pardon <span style="font-style: italic;">main-objects</span>, are fine without the <span style="font-style: italic;">decoration</span> but they&#8217;re much better with and the best thing is that you&#8217;re able to combine the <span style="font-style: italic;">decorations</span>&#8230; yummy</p>
<p>So that&#8217;s how this would look like more technically:<br />
<a href="http://bp2.blogger.com/_l5fIZzJyYfc/SCjU0tPibZI/AAAAAAAAAAU/uuR_geevSdw/s1600-h/decorator-pattern.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" rel="lightbox[558]"><img id="BLOGGER_PHOTO_ID_5199639771762290066" style="cursor: pointer;" src="http://bp2.blogger.com/_l5fIZzJyYfc/SCjU0tPibZI/AAAAAAAAAAU/uuR_geevSdw/s400/decorator-pattern.png" border="0" alt="" /></a></p>
<pre name="code" class="php">
stract class Cookie {
    protected $flavor;
    public function __construct($flavor) {
        $this->flavor=$flavor;
    }
    abstract public function descripeFlavor();
}

class GrannysCookie extends Cookie {
    public function descripeFlavor() {
        echo ‘<br/>Granny baked a cookie which has a taste of ’;
        echo $this->flavor;
    }
}

abstract class CookieDecorator extends Cookie {
    protected $cookie;
    public function __construct(Cookie $cookie) {
        $this->cookie = $cookie;
    }
    //abstract public function descripeFlavor();
}

class FreshCookieDecorator extends CookieDecorator {
    public function descripeFlavor() {
        $this->cookie->descripeFlavor();
        echo ‘ which smells fresh from the oven’;
    }
}

class CrumbleCookieDecorator extends CookieDecorator {
    public function descripeFlavor() {
        $this->cookie->descripeFlavor();
        echo ‘ it has tasty crumbles ’;
    }
}

$cookie = new GrannysCookie(‘chocolate’);
$cookie->descripeFlavor();

$crumbleCookie = new CrumbleCookieDecorator($cookie);
$crumbleCookie->descripeFlavor();

$freshCookie = new FreshCookieDecorator($cookie);
$freshCookie->descripeFlavor();

$freshAndCrumbledCookie = new FreshCookieDecorator($crumbleCookie);
$freshAndCrumbledCookie->descripeFlavor();
</pre>
<p><a href="http://en.wikipedia.org/wiki/Decorator_pattern">additional Information</a></p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.tolleiv.de/?flattrss_redirect&amp;id=558&amp;md5=bdbadaae7a469f4d3259bb4bf3635d85" 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/decorator-gof/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

