<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://dikini.net" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>dikini.net - design patterns</title>
 <link>http://dikini.net/taxonomy/term/101/0</link>
 <description></description>
 <language>en</language>
<item>
 <title>Models, records, databases, friends and foes</title>
 <link>http://dikini.net/models_records_databases_friends_and_foes</link>
 <description>&lt;p&gt;the beginnings of a &lt;em&gt;long&lt;/em&gt; on DB related pattern discussion.&lt;/p&gt;
</description>
 <comments>http://dikini.net/models_records_databases_friends_and_foes#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <pubDate>Fri, 27 Oct 2006 11:01:12 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">230 at http://dikini.net</guid>
</item>
<item>
 <title>Problems of active record and friends</title>
 <link>http://dikini.net/problems_of_active_record_and_friends</link>
 <description>&lt;p&gt;After a lot of silence caused by lot&#039;s of work (good, my bills are happy) and a continuous diversion into scheme, haskell, dylan and other interesting languages, I&#039;m back into php speaking land. Funny feeling that. So here comes the beginning of something I&#039;ve been continuously rubbing my (leftovers of) brains against.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;copout&lt;/strong&gt; This is not php specific, but since it discusses frequent problems occuring in php apps, I&#039;ve labeled it with as php related as well. Makes it easier maintaining the site you see.&lt;/p&gt;
&lt;p&gt;I have a problem with active record as a pattern. It is a logical one. Essentially it turn a (SQL) database row into an object. The class of the object represents the SQL table or view. Of course you can add behaviour to those classes and objects, i.e. activating those records.&lt;/p&gt;
&lt;p&gt;All that is nice and dandy, but when you start talking about relations and constructing &lt;em&gt;dynamically&lt;/em&gt; queries and corresponding objects, we start hitting the limitations of what I will call from now on &lt;em&gt;the  naive active record&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The problem is not trivial at all. It boils down to how to make peace between the host language type system (for example classes) and the SQL&#039;s dynamic compound types - the relations expressed as SELECT variants for example. It gets even harder when we decide to reverse the direction, so that we actulally want to update the database. Yuk! As though somebody actually does that!&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/problems_of_active_record_and_friends&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/problems_of_active_record_and_friends#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/series">series</category>
 <pubDate>Fri, 27 Oct 2006 06:25:45 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">228 at http://dikini.net</guid>
</item>
<item>
 <title>A catalogue of php design pattern shorts</title>
 <link>http://dikini.net/a_catalogue_of_php_design_pattern_shorts</link>
 <description>&lt;dl&gt;
&lt;dt&gt;What can you find on these pages?&lt;/dt&gt;
&lt;dd&gt;A pattern catalogue with php pseudo-codish examples. For working implementations you can always check &lt;a href=&quot;http://www.fluffycat.com/PHP-Design-Patterns&quot;&gt;what the kitty thinks of that&lt;/a&gt;. I might have problems with over implementation, but those are my problems, it does not mean those pages are notr worth as a reference.&lt;/dd&gt;
&lt;dt&gt;Why?&lt;/dt&gt;
&lt;dd&gt;A reminder and a tantrum space&lt;/dd&gt;
&lt;dt&gt;Why have you moved the catalogue to this page?&lt;/dt&gt;
&lt;dd&gt;Because I wanted to separate this catalogue from longer descriptions I plan to put, and all of them should live under design patterns&lt;/dd&gt;
&lt;dt&gt;Warnings and notices&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;&lt;strong&gt;warning&lt;/strong&gt; I realised, late enough not to correct it, that I&#039;m using a particular case of wishful thinking - using any php callable in &lt;code&gt;$callable(...)&lt;/code&gt; expressions. The reality is abit uglier. We need to use either
&lt;code&gt;call_user_func($callable,...); call_user_func_array($callable,...)&lt;/code&gt; How I wish php could do that out of the box, especially when it actually does it, just not in the short syntax. Anyone happy to hold my hand to do a php patch for that feature? I&#039;m very unfamiliar with the internals, sorry.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;notice&lt;/strong&gt; As Jared notes in his comment &lt;a href=&quot;http://dikini.net/26.07.2006/updated_design_pattern_collection#comment-16415&quot;&gt;calling functions/methods with $&lt;/a&gt; in a lot of cases you can use &lt;code&gt;$obj-&gt;$meth(...)&lt;/code&gt; instead of &lt;code&gt;call_user_func($callable,...);&lt;/code&gt;. Indeed you can use the following code as part of the remedy
&lt;code&gt;list($object,$method) = some_call()
$object-&gt;$method(...)&lt;/code&gt;
unfortunately this discriminates against functions, which are lighter to use than their method brethren&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;and a mention&lt;/strong&gt; The above two used to live on the top pattern page until I moved them here&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/a_catalogue_of_php_design_pattern_shorts&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/a_catalogue_of_php_design_pattern_shorts#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Fri, 27 Oct 2006 05:57:36 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">227 at http://dikini.net</guid>
</item>
<item>
 <title>macros, higher-order functions, datatypes and design patterns</title>
 <link>http://dikini.net/29.08.2006/macros_higher_order_functions_datatypes_and_design_patterns</link>
 <description>&lt;p&gt;While reading, researching and experimenting for my macros project the terms in the title get mixed a lot. I&#039;ll try to summarise what is my understanding of a lot of the above. To be fair, most of these words are heavily overloaded with sometimes contradictory and or ovelapping meanings, so this short is only about my personal summary, no pretence for a general study or lit review.&lt;/p&gt;
&lt;p&gt;Generic programming intuitevely corresponds to design patterns, or a tool to implement them. There should be a distinction between the functional or algorithmic side of the term and the structural or data (type) genericity.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/29.08.2006/macros_higher_order_functions_datatypes_and_design_patterns&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/29.08.2006/macros_higher_order_functions_datatypes_and_design_patterns#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/dylan">dylan</category>
 <category domain="http://dikini.net/tags/haskell">haskell</category>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <pubDate>Tue, 29 Aug 2006 10:09:21 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">215 at http://dikini.net</guid>
</item>
<item>
 <title>Poor man&#039;s macro programming in php (revisited)</title>
 <link>http://dikini.net/01.08.2006/poor_mans_macro_programming_in_php_revisited</link>
 <description>&lt;em&gt;How exactly can you do macros in php now?&lt;/em&gt;

&lt;p&gt;Macros have (let&#039;s say) two main responsibilities - adding syntactic sugar and abstraction of common patterns. The syntactic sugar bit is something which probably should happen at compile time, at least in a weakly typed language like php. So I&#039;ll skip them. The abstraction of computation, well this is wat codified design patterns are. This is what function, classes and the rest of the things we read about and use usually do. To abstract a function in php there are at least two different strategies, probably more, but these two are effective in diametrically different situations. The first one is a combination of the ideas of &lt;a href=&quot;http://dikini.net/generic_function&quot;&gt;generic functions (higher order functions)&lt;/a&gt; and &lt;a href=&quot;http://dikini.net/partial_evaluation&quot;&gt;partial evaluation&lt;/a&gt;. In code it can look something like:&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://dikini.net/01.08.2006/poor_mans_macro_programming_in_php_revisited&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/01.08.2006/poor_mans_macro_programming_in_php_revisited#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/ideas">ideas</category>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <pubDate>Tue, 01 Aug 2006 04:31:59 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">207 at http://dikini.net</guid>
</item>
<item>
 <title>Proxy</title>
 <link>http://dikini.net/proxy</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Provide a surrogate or placeholder for another object to control access to it&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to provide a wrapper around a set of classes to unify their access protocols&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;&lt;ul&gt;&lt;li&gt;Provide a wrap method&lt;/li&gt;
&lt;li&gt;intercept calls to the wraper object and forward them accordingly, may change the protocol&lt;/li&gt;
&lt;/ul&gt;&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class Fluenter
{
        private $obj;

        function __construct($obj)
        {
                $this-&gt;obj = $obj;
        }
        static function MakeFluent($obj)
        {
                if ($obj instanceof fluent)
                        return $obj;
                else
                        return new fluenter($obj);
        }
        function __call($method, $args)
        {
                $result = call_user_func_array(array($this-&gt;obj, $method), $args);
                if (is_null($result))
                        return $this;
                else if (is_object($result) and ($result instanceof $fluent))
                        return $result;
                else
                        throw new RuntimeException(
                                                &quot;Fluent::__call called method $method &quot;.
                                                &quot;and expected a null return or a non-fluent object, &quot;.
                                                &quot;got (&quot;.gettype($return).&quot;) $return instead.&quot;);
        }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;&lt;li&gt;This code is an unaltered copy from &lt;a href=&quot;http://blog.jonnay.net/archives/599-Making-Fluent-Interfaces-Readable-in-PHP.html&quot;&gt;Making Fluent Interfaces Readable in PHP&lt;/a&gt;, I hope Jonnay doesn&#039;t mind that&lt;/li&gt;
&lt;li&gt;You might want to check out &lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;fluent interfaces( method chaining)&lt;/a&gt; and &lt;a href=&quot;http://dikini.net/accumulator_passing&quot;&gt;accumulator passing&lt;/a&gt;&lt;/li&gt;&lt;/dd&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/proxy&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/proxy#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Thu, 27 Jul 2006 11:20:04 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">206 at http://dikini.net</guid>
</item>
<item>
 <title>Accumulator passing</title>
 <link>http://dikini.net/accumulator_passing</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Use of an accumulator variable to allow the finer control over the assembly of the final result of an algorithm &lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to use &lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;method-chaining&lt;/a&gt;, but we need to return values from some of the methods&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use an accumulator variable&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;  a simple IFS( iterative function system ) &lt;br /&gt;
  x(t+1) = x(t) + f[random(1..i)]( 1/(t+1) ) &lt;br /&gt;
  note: the implementation is quite naive and not foolproof at all.
&lt;pre&gt;&lt;code&gt;&amp;lt;php
class example {
  private $ifs = array( &#039;sin&#039;, &#039;cos&#039; );  

  function x( &amp;$acc ) {
    $acc[&#039;t&#039;] += 1;
    $acc[&#039;x&#039;] = $acc[&#039;x&#039;] + $this-&gt;ifs[ rand(0,1) ]( 1/ $acc[&#039;t&#039;] );
    return $this;
  }
}

$ex = new example();

$something = array(&#039;t&#039;=&gt;0, &#039;x&#039; =&gt; 0 );
$ex-&gt;x($something) -&gt; x($something) -&gt; x($something);
print &#039;t: &#039; . $something[&#039;t&#039;] . &#039; x: &#039; . $something[&#039;x&#039;] . &quot;\n&quot;;
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
and when you run it:
&lt;pre&gt;&lt;code&gt;vlado@cow:~/php design patterns$ php  php accumulator_passing.php
t: 3 x:1.34692254127
vlado@cow:~/php design patterns$ php accumulator_passing.php
t: 3 x: 1.64809122021
vlado@cow:~/php design patterns$ php accumulator_passing.php
t: 3 x: 2.66401049301
&lt;/code&gt;&lt;/pre&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/accumulator_passing&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/accumulator_passing#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Thu, 27 Jul 2006 10:59:21 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">205 at http://dikini.net</guid>
</item>
<item>
 <title>Fluent Interfaces/ Method chaining</title>
 <link>http://dikini.net/fluent_interfaces_method_chaining</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;return the next context of computation, enable simple and flexible sequencing of computation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to provide a small domain specific language to improve the readability of the code&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;return the next expected object (state)&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class fluent_class {
  var $state_var;
  function self_next( $arg ) {
    ....
    return $this;
  }
  function other_next( $arg ) {
    ....
    return $this-&gt;another;
  }
}

$obj = new fluent_class();
$obj-&gt;self_next(1)
    -&gt;self_next(2)
    -&gt;other_next(3)
    -&gt;something_else(4);

&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;This is an awkward idea to explain in words. Essentially you are delegating the desision what is the next object in the chain to the executable methods. It probably should be combined with weaving the values, which we would otherwise return, using accumulator style passing, or some other store method.&lt;/dd&gt;
&lt;dd&gt;Have a look at &lt;a href=&quot;http://blog.jonnay.net/archives/599-Making-Fluent-Interfaces-Readable-in-PHP.html&quot;&gt; Jonnay&#039;s post&lt;/a&gt; for better and more sensible code examples and the Fluenter class.&lt;/dd&gt;
&lt;dd&gt;I sometimes look at this as the imperative &quot;version&quot; of continuation passing, but let&#039;s not even get there.&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/fluent_interfaces_method_chaining#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Thu, 27 Jul 2006 07:33:18 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">204 at http://dikini.net</guid>
</item>
<item>
 <title>Googles Summer of yawn.</title>
 <link>http://dikini.net/26.07.2006/googles_summer_of_yawn</link>
 <description>&lt;blockquote&gt;&lt;p&gt;&lt;a href=&quot;http://blog.jonnay.net/archives/739-Googles-Summer-of-yawn..html&quot; rel=&quot;nofollow&quot;&gt;Googles Summer of yawn.&lt;/a&gt; - &lt;em&gt;Google&#039;s &amp;quot;Summer of Code&amp;quot; has started and they have announced a few PHP projects. Some of them look interesting and useful.  However, I am going to pick apart one in particular, that to be honest, looks neither interesting or useful: &lt;a href=&quot;http://code.google.com/soc/php/appinfo.html?csaid=3752FBA8CFFCD528&quot; rel=&quot;nofollow&quot;&gt;The PHP Macro Preprocessor&lt;/a&gt;.&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
Why does the PHP Preprocessor need to be stuck in a world of #ifdefs and #includes?  Instead of blindly copying what the C preprocessor does, why not focus on the languages strengths and deficiencies?  The object system of PHP needs to be looked at, and see if it can be improved upon through a method of code transformation.&lt;br /&gt;
&lt;br /&gt;&lt;/em&gt; [&lt;a href=&quot;http://blog.jonnay.net/&quot; rel=&quot;nofollow&quot;&gt;sacrificial rabbit&lt;/a&gt;]
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;I sooo totally agree with Jonnay on this. In fact it is simply like reading my own thoughts, just put it better english, , ok, nearly. But what to do about it? I don&#039;t really know, I&#039;m afraid I chicken out from digging into the guts of php. When I last looked there it was really scary.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/26.07.2006/googles_summer_of_yawn&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/26.07.2006/googles_summer_of_yawn#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/rants">rants</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <pubDate>Wed, 26 Jul 2006 09:07:08 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">202 at http://dikini.net</guid>
</item>
<item>
 <title>Updated design pattern collection</title>
 <link>http://dikini.net/26.07.2006/updated_design_pattern_collection</link>
 <description>&lt;p&gt;I guess I&#039;m on the down form my design pattern tantrum hit. I updated the collection so  it now has about 20 of them, not all can be considered &quot;officially&quot; design patterns, but to be honest I don&#039;t really care. I&#039;m very well aware that I ignore the existance of boundaries between design patterns, idioms, techniques, ... That is intentional. I like my rainbow coloured world and I want to keep it that way. If you disagree - just read the small script. My tanrum energy is over and I need to do work work. So the combing over the &lt;a href=&quot;http://dikini.net/php_design_patterns&quot;&gt;php design pattern collection&lt;/a&gt; will come later. For now enjoy, and have anger fits of your own.&lt;/p&gt;
&lt;p&gt;Oh yes. I&#039;m using a particular case of wishful thinking - using any php callable in $callable(...) expressions. The reality is a bit uglier. We need to use either call_user_func($callable,...); call_user_func_array($callable,...) How I wish php could do that out of the box, especially when it actually does it, just not in the short syntax. Anyone happy to hold my hand to do a php patch for that feature? I&#039;m very unfamiliar with the internals, sorry.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/26.07.2006/updated_design_pattern_collection&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/26.07.2006/updated_design_pattern_collection#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/ideas">ideas</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <pubDate>Wed, 26 Jul 2006 06:22:18 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">201 at http://dikini.net</guid>
</item>
<item>
 <title>Coroutines (generator based)</title>
 <link>http://dikini.net/coroutines_generator_based</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Abstract out naturally parrallel evaluation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Merge two functional streams&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;The coroutines can be implemented using generators. The example shows an example application&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;//if gen and gen2 are generators
function merge_data() {
  try {
    do {
      $data[] = 10*gen() + 5 + 4*gen2();
      ...
    } while( some_condition( ... ) );
  } catch ( $e ) { .... }
  return $data;
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/coroutines_generator_based&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/coroutines_generator_based#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:50:22 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">200 at http://dikini.net</guid>
</item>
<item>
 <title>Generator (iterator protocol based)</title>
 <link>http://dikini.net/generator_iterator_protocol_based</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Define a (stream like) value producer&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to be able do define functional streams&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Any object implementing an iteration protocol (interface) can be a base for a generator.&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
//a generator implemented with iterators
function generator($it=null, $cb=null) {
  static $itor;
  static $callback;

  if(!isset($itor)) ($itor = $it; $callback=$cb; }

  if($valid()) { $out = $callback($current); $next(); return $out; }

  //or if you are not sure about the type of the above callables ($valid, $next,..)
  if( call_user_func($valid) ) { 
    $out = call_user_func( $callback, $current); 
    call_user_func( $next ); 
    return $out; 
  }

  else throw( new some_exception );
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/generator_iterator_protocol_based&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/generator_iterator_protocol_based#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:46:42 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">199 at http://dikini.net</guid>
</item>
<item>
 <title>Iterator (based on protocol method)</title>
 <link>http://dikini.net/iterator_based_on_protocol_method</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to be able to postpone the effects of a computation, for example delay printing from a function to webserver output until whole web-page is assembled&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;This is an alternative implementation based on &lt;a href=&quot;http://dikini.net/protocol_method/protocol method&quot;&gt;protocol method&lt;/a&gt;, rather than interfaces&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class alt_iterator {
  private $st = array();

  function next() { ... }
  function current() { ... }
  function valid() { ... }
  //... put the rest here

  function protocol() {
    return array(
             array($this,&#039;next&#039;),
             array($this,&#039;current&#039;),
             array($this,&#039;valid&#039;),
	     .....
	     );
  }
  
}

//an example use
function do_them($itor, $callback ) {
  list($next, $current, $valid, ...) = $itor-&gt;protocol();
  while($valid) {
    $callback( $current );
    $next();
  }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/iterator_based_on_protocol_method&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/iterator_based_on_protocol_method#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:31:04 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">198 at http://dikini.net</guid>
</item>
<item>
 <title>Dynamic dispatch</title>
 <link>http://dikini.net/dynamic_dispatch</link>
 <description>&lt;dl&gt;
&lt;dt&gt;Intent&lt;/dt&gt;
&lt;dd&gt;dispatch the control based on the environment, but delay enumerating the cases&lt;/dd&gt;
&lt;dt&gt;Motivation&lt;/dt&gt;
&lt;dd&gt;we don&#039;t know at the time of writing the code what are the different cases to dispatch on&lt;/dd&gt;
&lt;dt&gt;Implementation&lt;/dt&gt;
&lt;dd&gt;(nearly) invisible in php&lt;/dd&gt;
&lt;dt&gt;Examples&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;$case($arg);&lt;br /&gt;
$object-&amp;gt;$method($arg);&lt;br /&gt;
call_user_func($calleable, $arg1, $arg2);&lt;br /&gt;
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;A more elaborate form of dynamic dispatch is &lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;Method Chaining&lt;/a&gt;&lt;/dt&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/dynamic_dispatch&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/dynamic_dispatch#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:23:19 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">197 at http://dikini.net</guid>
</item>
<item>
 <title>Lazy evaluation</title>
 <link>http://dikini.net/lazy_evaluation</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;A function/method call results in a promise for future computation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to be able to postpone the effects of a computation, for example delay printing from a function to webserver output until whole web-page is assembled&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Instead of evaluating a function, we deliver a promise - a callable with no arguments, which when forced is executed&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
//for simplicity handle only functions with one argument 
class promise { 
  private $args = null;
  private $func = null;
  function __construct($func, $args) { 
    $this-&gt;func = $func; 
    $this-&gt;args = $args;
  } 
  function evaluate() { 
    call_user_func($this-&gt;func,$this-&gt;args);
  } 
} 

function delay( $func, $arg ) {
  return array(new promise($func, $arg), &#039;evaluate&#039;); 
}

//example use
...
$chunks[] = delay(&#039;a_printer&#039;,$a_variable);
...
foreach($chunks as $chunk ) {
  if(is_callable($chunk)) {
    $chunk(); //force - this does it&#039;s own printing in this example
  } else {
    print $chunk;
  }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/lazy_evaluation&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/lazy_evaluation#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:10:57 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">196 at http://dikini.net</guid>
</item>
<item>
 <title>Nested scope</title>
 <link>http://dikini.net/nested_scope</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Create a composable runtime hierarchy of data and behaviour&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want dynamic means of modifying complex runtime structures&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use php &lt;em&gt;magic&lt;/em&gt; to dynamically dispatch requests to parent methods&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class scope {
  var $parent;
  function __call( $name ) {
    if(isset($parent[$name]) { return $parent[$name](); }
    throw( new someException() );
  }

  function let( $name, $value ) {
    if(is_object($value)) { 
      //hygiene
      if(isset($this-&gt;$name)) { $this-&gt;$name-&gt;parent = null; }
      $this-&gt;$name = $value;
      $this-&gt;$name-&gt;parent = $this;
  }
}

//alternative let
function let($parent, $name, $class) {
  $parent-&gt;$name = new $class();
  $parent-&gt;$name-&gt;parent = array($parent,$name);
}

?&gt;

&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;&lt;li&gt;A concept related to and very often mistaken (mixed) (by me) with (for) &lt;a href=&quot;http://dikini.net/closure&quot;&gt;closures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;the let method can be folded into &lt;em&gt;__set&lt;/em&gt; if we want this behaviour to the default for assignments to object fields&lt;/li&gt;&lt;/ul&gt;&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/nested_scope&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/nested_scope#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 04:46:50 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">195 at http://dikini.net</guid>
</item>
<item>
 <title>Closure</title>
 <link>http://dikini.net/closure</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Create first class behaviours capable of storing state between use&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to maintain the history of computation, an maybe save and reuse the computation at a particular point in time&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;The basic implementation is invisible in php - &lt;em&gt;static variables in functions&lt;/em&gt; (but that doesn&#039;t allow us to have the usual features of a language with closures because functions in php are not first class objects, i.e we can&#039;t copy them), &lt;em&gt;objects&lt;/em&gt; (they get us there, albeit more verbously). 
The examples below are borrowed from &lt;a href=&quot;http://ioreader.com/2007/05/03/php-closures/&quot;&gt;io-reader: php closures&lt;/a&gt;. Read the post and the full code to understand the full implementation.&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;// fixed-point combinator
function Y(Lambda &amp;$le) {
	return lambda(get_defined_vars(), &#039;Lambda $f&#039;, &#039;
		return $f-&gt;call($f);
	&#039;)-&gt;call(lambda(get_defined_vars(), &#039;Lambda $f&#039;, &#039;
		return $le-&gt;call(lambda(get_defined_vars(), \&#039;$x\&#039;, \&#039;
			return $f-&gt;call($f)-&gt;call($x);
		\&#039;));
	&#039;));
}
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;Closures and objects are dual, compare &lt;em&gt;behaviour with state&lt;/em&gt; and &lt;em&gt;state with behaviours&lt;/em&gt;&lt;/dd&gt;
&lt;dt&gt;Yet another note:&lt;/dt&gt;
&lt;dd&gt;Alternatively have a look at &lt;a href=&quot;http://www.steike.com/code/php-closures/&quot;&gt;this implementation&lt;/a&gt; or &lt;a href=&quot;http://dikini.net/24.01.2006/emulating_closures_in_php&quot;&gt;my emulating closures in php blurb&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;Final note:&lt;/dt&gt;
&lt;dd&gt;All this is better avoided in php - too many underwater rocks&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/closure&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/closure#comment</comments>
 <category domain="http://dikini.net/tags/closures">closures</category>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 04:29:49 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">194 at http://dikini.net</guid>
</item>
<item>
 <title>State</title>
 <link>http://dikini.net/state</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Allow an object to alter its behavior when its internal state changes. &lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;An article in a typical editorial workflow exibits different behaviours in different publication stages. We want an object that hides the complex logic.&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Similar to the strategy pattern. Need to handle state change and behaviour change.&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
$new_state=&quot;edit&quot;;
$new_protocol=&quot;edit&quot;;

//change the behaviour
list($view,$update,$get, $set) = $object-&gt;protocol($new_protocol);
&lt;/code&gt;&lt;/dd&gt;
 &lt;dt&gt;Related patterns&lt;/dt&gt;
 &lt;dd&gt;&lt;a href=&quot;http://dikini.net/protocol_method&quot;&gt;Protocol Method&lt;/a&gt;, Builder, &lt;a href=&quot;http://dikini.net/strategy_pattern&quot;&gt;Strategy&lt;/a&gt;&lt;/dd&gt;
 &lt;dt&gt;Note&lt;/dt&gt;
 &lt;dd&gt;Too many different implementations exists. This means that there is probably a need to clean up the intent.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;&lt;a href=&quot;http://dikini.net/state&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/state#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 12:41:07 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">193 at http://dikini.net</guid>
</item>
<item>
 <title>Observer</title>
 <link>http://dikini.net/observer</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;When an object changes, notify all interested parties&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;events, synchronisation, ...&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use &lt;a href=&quot;http://dikini.net/hooks&quot;&gt;hooks pattern&lt;/a&gt; &lt;em&gt;at&lt;/em&gt; and &lt;em&gt;after&lt;/em&gt; to implement the observer&lt;/dd&gt;
 &lt;dd&gt;No implementation needed in the Subject class&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
//initialisation
$hs = new hooks();
$observer = new interested_party();
$change_method = array($object, &#039;setter&#039;);

//setup change point(can be done externally from $change method)
$hs-&gt;at(&#039;change&#039;, $change_method);

//express interest
$hs-&gt;after(&#039;change&#039;, array($obesrver, &#039;callback&#039;));

$hs-&gt;run($arg);
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;The precise implementation of the hooks may differ, depending on functionality, abstraction, sugar, ... Have a look at the drupal hooks as well.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/observer&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/observer#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:54:56 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">192 at http://dikini.net</guid>
</item>
<item>
 <title>Hooks</title>
 <link>http://dikini.net/hooks</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Build a function/method from multiple components&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Very often we need to decouple or delay definitions until run time&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Implement a methods that allows executing a set of functions at predefined times&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;class hooks {
  private $cache = array();

  function run( $func, $arg ) {
    foreach(array(&#039;before&#039;,&#039;at&#039;,&#039;after&#039;) as $hook)
      foreach($this-&gt;cache[$func][$hook] as $f) { $f($arg);  }
  }

  function hook($hook, $func, $cb) {
    $this-&gt;cache[$func][$hook][]=$cb
  }

  function before($func, $cb) {
    $this-&gt;cache[$func][&#039;before&#039;][]=$cb
  }
  
  function at($func, $cb) {
    $this-&gt;cache[$func][&#039;at&#039;][]=$cb
  }
  
  function after($func, $cb) {
    $this-&gt;cache[$func][&#039;after&#039;][]=$cb
  }
  
  function around($hook, $func, $cb) {
    $this-&gt;cache[$func][&#039;around&#039;][]=$cb
    $this-&gt;cache[$func][&#039;after&#039;][]=$cb
  }

}

//and usage
$hs = new hooks();
$hs-&gt;hook(&#039;before&#039;,&#039;label&#039;,&#039;before_a_func&#039;);
$hs-&gt;hook(&#039;at&#039;,&#039;label&#039;,&#039;core&#039;);
$hs-&gt;run();

//or using the individual methods
$hs = new combinator();
$hs-&gt;before(&#039;label&#039;,&#039;before_a_func&#039;);
$hs-&gt;around(&#039;label&#039;,&#039;tracer&#039;);
$hs-&gt;run();
&lt;/code&gt;&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/hooks&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/hooks#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:41:04 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">191 at http://dikini.net</guid>
</item>
<item>
 <title>Dynamic loading</title>
 <link>http://dikini.net/dynamic_loading</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Keep working set small and mainatin fast start time by loading features as required&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;use &lt;em&gt;__autoload()&lt;/em&gt;&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;function autoload( $classname) {
  list($path,$file) = loadpath( $classname );
  require_once &quot;$path/$file&quot;;
}&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;
&lt;li&gt;we are loading a file, based on some conventions interpreted in loadpath&lt;/li&gt;
&lt;li&gt;This can be used to implement module-like system, but the lack of subclasses or similar tools makes the &#039;module&#039; bindings always transparent&lt;/li&gt;
&lt;/ul&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/dynamic_loading&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/dynamic_loading#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:21:47 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">190 at http://dikini.net</guid>
</item>
<item>
 <title>Control abstraction</title>
 <link>http://dikini.net/control_abstraction</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Replace loops with named function&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;find the best value in an array&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;A simple loop over the array, keeping track of the best element value and index&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;function find_best($better, $coll) {
  foreach($coll as $key=&gt;$value) {
    if( is_null($best) || $better($value,$best[1]) ) {
      $best = array($key, $value);
    }
  }
  return $best;
}&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/control_abstraction&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/control_abstraction#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:13:32 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">189 at http://dikini.net</guid>
</item>
<item>
 <title>Protocol method</title>
 <link>http://dikini.net/protocol_method</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Implement a set of related operations&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Define different iteration strategies, across different inheritance hierarchies, similar to interface, but allows for differnt fuctions to implement the interface specification&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Define a method returning the required functions by the interface&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;class alt_iterator {
  private $st = array();

  function next() { ... }
  function current() { ... }
  function valid() { ... }
  //... put the rest here

  function protocol() {
    return array(
             array($this,&#039;next&#039;),
             array($this,&#039;current&#039;),
             array($this,&#039;valid&#039;),
	     .....
	     );
  }  
}

//an example use: iterator protocol
function do_them($itor, $callback ) {
  list($next, $current, $valid, ...) = $itor-&gt;protocol();
  while($valid) {
    $callback( $current );
    $next();
  }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/protocol_method&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/protocol_method#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:02:41 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">188 at http://dikini.net</guid>
</item>
<item>
 <title>Interpreter</title>
 <link>http://dikini.net/interpreter</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;given a language, interpret sentences&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;request (path) parser in a web application&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;&lt;ul&gt;&lt;li&gt;a class for each expression type, interpret method for each type&lt;/li&gt;
&lt;li&gt;a class and object to store state (context)&lt;/li&gt;
&lt;li&gt;each expression class is resposible for building it&#039;s own parse tree&lt;/li&gt;
&lt;/ul&gt;&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;class tag {
  //ts is the current token stream
  //this is a variant of partial evaluation
  var $args;
  function __construct( &amp;$ts) { 
    while( not_class($arg = next($ts)) ) {
      $this-&gt;args[] = $arg;
    }
  }
  //evaluate the 
  function run( &amp;$context) { ... }
}

//example usage
$ts = array(&quot;tag&quot;,&quot;php&quot;,&quot;design patterns&quot;);
$op = current($ts);
$prog = new $op();
$prog-&gt;run( new context() );
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;&lt;li&gt;the expression types can be possibly parametrized if their run an construct methods follow, particular &#039;typical&#039; shapes&lt;/li&gt;
&lt;li&gt;not_class is a language dependent function&lt;/li&gt;&lt;/dd&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/interpreter&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/interpreter#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 06:52:29 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">187 at http://dikini.net</guid>
</item>
<item>
 <title>Generic function</title>
 <link>http://dikini.net/generic_function</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;A high-order function/algorithm implementation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;a generic select-&gt;update-&gt;eval function with one argument, whose instances/application differ for different (argument, result) types required&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Simply cache the result into an array or other appropriate type&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class algorithm {
   var $parameters=array();
   function __construct( $select, $update, $eval) {
     $this-&gt;parameters[&#039;select&#039;] = $select;
     $this-&gt;parameters[&#039;update&#039;] = $update;
     $this-&gt;parameters[&#039;eval&#039;] = $eval;
   }
   function run($arg) {
     $selections = $this-&gt;parameters[&#039;select&#039;]($arg);
     $updates = $this-&gt;parameters[&#039;update&#039;]($selections);
     return $this-&gt;parameters[&#039;eval&#039;]($updates);
   }
}

//or with more syntax, not the parametrisation
function algorithm( $select, $update, $eval ) {
   $args = func_get_args();
   $args = array_slice($input, 3);
   $selections = call_user_func_array( $select, $args);
   $updates = call_user_func_array( $update, $selections);
   return call_user_func_array( $eval, $updates);
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/generic_function&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/generic_function#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 06:37:57 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">186 at http://dikini.net</guid>
</item>
<item>
 <title>Partial Evaluation</title>
 <link>http://dikini.net/partial_evaluation</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Produce a new function, by caching one or more of the original arguments&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Very often we use a function with one of it&#039;s complex arguments over and over again. We want to speed the avaluation.&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use &lt;a href=&quot;http://dikini.net/memoization&quot;&gt;memoization&lt;/a&gt; to remember the argument&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
&lt;code&gt;class p_eval {
  private $args = null;
  private $func = null;
  function __construct($func,$args) {
    $this-&gt;func = $func;
    $this-&gt;args = $args
  }

  function evaluate($arg) {
    call_user_func($this-&gt;func,array_push($this-&gt;args,$arg));
  }
}

//usage
$f = array(new p_eval(&#039;a_function&#039;,array(&#039;one&#039;)), &#039;evaluate&#039;);
....
$f($arg);&lt;/code&gt;&lt;/dd&gt;

&lt;p&gt;&lt;a href=&quot;http://dikini.net/partial_evaluation&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/partial_evaluation#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 06:24:24 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">185 at http://dikini.net</guid>
</item>
<item>
 <title>Facade</title>
 <link>http://dikini.net/facade</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;provide a simple interface to a subsystem.&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;a complex system can have many parts that need to be exposed, but it could be confusing ergo - provide a simple interface to the system. For example a read-evaluate-print process of an interpreter&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Hide the construction of the required objects inside the facade&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;
&lt;code&gt;class rep_facade {
   function __construct() {
     $this-&gt;firewall = new firewall();
     $this-&gt;reader = new reader();
     $this-&gt;evaluator = new evaluator();
     $this-&gt;printer = new printer();
   }

   function rep($text) {
     $text = $this-&gt;firewall($text);
     $ts   = $this-&gt;reader($text);
     $result = $this-&gt;evaluator($ts);
     return $this-&gt;printer($result);
   }
 }&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Complications&lt;/dt&gt;
&lt;dd&gt;The lack of modules and/or nested classes in php means that this is a comfort interface, not an enforcing interface&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/facade&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/facade#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 05:53:42 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">184 at http://dikini.net</guid>
</item>
<item>
 <title>Factory and Abstract Factory</title>
 <link>http://dikini.net/factory_and_abstract_factory</link>
 <description>&lt;?php

&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Creating objects (products) without specifying the exact class(type) of object that will be created.&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want our program logic to determine the type of logger to be used&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Classes in php can be considered (weak) runtime objects, the nessessity of the dual factory/product heirarchy is diminished&lt;/dd&gt;
 &lt;dd&gt;Invisible in php&lt;/dd&gt;
 &lt;dd&gt;Might still want factory objects to handle families of similar objects&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
$product_type = which_product();
$probuct = new $product_type();
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/factory_and_abstract_factory&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/factory_and_abstract_factory#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 05:20:37 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">183 at http://dikini.net</guid>
</item>
<item>
 <title>Singleton</title>
 <link>http://dikini.net/singleton</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;enforce the existance of a single object of a specified type&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;
  &lt;ul&gt;
   &lt;li&gt;use the &lt;a href=&quot;http://dikini.net/memoization&quot;&gt; memoize pattern&lt;/a&gt; to implement a singleton&lt;/li&gt;
  &lt;/ul&gt;
 &lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;with functions&lt;br /&gt;
&lt;code&gt;function &amp;get_instance() {
  static $instance;
  if(empty($instance)) {
    $instance = new SingletonClass();
  }
  return $instance;
}&lt;/code&gt;&lt;/dd&gt;
 &lt;dd&gt;with objects&lt;br /&gt;
&lt;code&gt;class Singleton {
  private static $instance = null;

  private static function __construct() {  }

  public  function get_instance() {
    if(empty(self::$instance) { self::$instance = new Singleton(); }
    return self::$instance;
  }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Complications&lt;/dt&gt;
&lt;dd&gt;The function based version implements the code equivalent of security by obscurity. It doesn&#039;t really enforce a single instance of a class, but a revised version can be used to abstract out singleton creation&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/singleton&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/singleton#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 05:09:08 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">182 at http://dikini.net</guid>
</item>
<item>
 <title>Memoization</title>
 <link>http://dikini.net/memoization</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Cache result after computing it.&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Eliminate repeated database queries&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Simply cache the result into an array or other appropriate type&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;with functions&lt;br /&gt;
&lt;code&gt;function db_get_something( $x ) {
  static $cache = array();

  if(empty($cache[$x])) { 
    $cache[$x] = //some db query result;
  }
  return $cache[$x];
}&lt;/code&gt;&lt;/dd&gt;
 &lt;dd&gt;with objects&lt;br /&gt;
&lt;code&gt;class db_get_somethning {
  private $cache = array();

  function func() {
    if(empty($this-&gt;cache[$x])) 
      $this-&gt;cache[$x] = //some db query result;
    return $this-&gt;cache[$x];
  }
}&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Complications&lt;/dt&gt;
&lt;dd&gt;cache lifetime, size, invalidation&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/memoization&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/memoization#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 04:36:36 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">181 at http://dikini.net</guid>
</item>
</channel>
</rss>
