<?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>Open Source Web Thoughts &#187; mvc</title>
	<atom:link href="http://blog.dewaldbotha.co.za/category/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dewaldbotha.co.za</link>
	<description></description>
	<lastBuildDate>Wed, 08 Apr 2009 11:17:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>game theory pattern code example</title>
		<link>http://blog.dewaldbotha.co.za/2009/02/17/game-theory-pattern-code-example/</link>
		<comments>http://blog.dewaldbotha.co.za/2009/02/17/game-theory-pattern-code-example/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 08:50:50 +0000</pubDate>
		<dc:creator>dewaldbotha</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[game theory]]></category>

		<guid isPermaLink="false">http://blog.dewaldbotha.co.za/2009-02-17/game-theory-pattern-code-example/</guid>
		<description><![CDATA[so i finally took the time to write some code to better articulate my thoughts on &#8216;game theory pattern&#8217;.

/*
* Abstract Unifier class to select objects from different sources.
*/

abstract class childObjectUnifier
{
    protected $currentObjectLocation;

    public function __construct()
    {
        $this->currentObjectLocation = get_class($this);
 <a href="http://blog.dewaldbotha.co.za/2009/02/17/game-theory-pattern-code-example/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>so i finally took the time to write some code to better articulate my thoughts on &#8216;game theory pattern&#8217;.</p>
<pre lang="php">
/*
* Abstract Unifier class to select objects from different sources.
*/

abstract class childObjectUnifier
{
    protected $currentObjectLocation;

    public function __construct()
    {
        $this->currentObjectLocation = get_class($this);
    }

    public function getData()
    {
        switch ($this->currentObjectLocation)
        {
             case 'currentServerLoginObject':
                    return $this->currentServerLoginFunction();
                    break;

             case 'remoteServerLoginObject':
                    return $this->remoteServerLoginFunction();
                    break;

             default:
                    die('Current user load not available');
        }
    }
}

/*
* Login Object which exists on the current server.
* This object could still be hit on the current server if the current user load is not high.
* Better utilizes the current server.
*/

class currentServerLoginObject extends childObjectUnifier
{
    public function currentServerLoginFunction()
    {
         return 'Some function on the current server';
    }
}

/*
* This login object could exist remotely, for e.g. a restfull/soap/xml-rpc service
* which could be hit if the current server load is climbing.
* Could possibly use a dedicated 'login' server if user load is exceedingly high on current machine.
*/

class remoteServerLoginObject extends childObjectUnifier
{
    public function remoteServerLoginFunction()
    {
        return 'Some function on the remote server';
    }
}

/*
* The game theory pattern which decides which objects to instantiate based on logical
* decisions made by solid data.
* In this example the current user load.
*/

class GameTheoryPattern
{
    public $currentObject;

    function __construct($iCurrentUserLoad)
    {
        $this->currentObject = $this->baseObjectUponStrategicData($iCurrentUserLoad);
    }

    private function baseObjectUponStrategicData($iCurrentUserLoad)
    {
         switch ($iCurrentUserLoad)
         {
             case $iCurrentUserLoad < 1000:
                    return new currentServerLoginObject();
                    break;

             case $iCurrentUserLoad >= 1000 :
                    return new remoteServerLoginObject();
                    break;

             default:
                    die('Current user load not available');
        }
    }
}

//GetCurrentUserLoad
$iCurrentUserLoad = 1000;

//Create Login Object based on the Game Theory Pattern
$oLogin = new GameTheoryPattern($iCurrentUserLoad);

//Call an function from the unifier class
echo $oLogin->currentObject->getData();

?></pre>
<p>It should be pretty self explanatory &#8211; due to the high level comments <img src='http://blog.dewaldbotha.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>feel free to add suggestions or comments &#8211; since i&#8217;m pretty sure there could be a better way, but for now, just to convey the message it should be sufficient.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dewaldbotha.co.za/2009/02/17/game-theory-pattern-code-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>applying game theory patterns to development</title>
		<link>http://blog.dewaldbotha.co.za/2009/02/11/applying-game-theory-patterns-to-development/</link>
		<comments>http://blog.dewaldbotha.co.za/2009/02/11/applying-game-theory-patterns-to-development/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 07:29:23 +0000</pubDate>
		<dc:creator>dewaldbotha</dc:creator>
				<category><![CDATA[architecture]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[game theory pattern]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://blog.dewaldbotha.co.za/2009-02-11/applying-game-theory-patterns-to-development/</guid>
		<description><![CDATA[mobile &#8211; that damned device that makes our life so easy, yet sometimes so inheritely difficult.
as a developer, we kind of try and convince ourselves that developing for mobile and developing for a desktop browser is kind of the same thing.  but we all know that this is a stalling technique for the inevitable, since <a href="http://blog.dewaldbotha.co.za/2009/02/11/applying-game-theory-patterns-to-development/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>mobile &#8211; that damned device that makes our life so easy, yet sometimes so inheritely difficult.</p>
<p>as a developer, we kind of try and convince ourselves that developing for <a href="http://mobiforge.com" title="Mobiforge" target="_blank">mobile</a> and developing for a desktop browser is kind of the same thing.  but we all know that this is a stalling technique for the inevitable, since invariably it becomes a whole different field of play.</p>
<p>every little feature you add, every little flow created and every branch of navigational hierarchy is a challenge on its own.</p>
<p><strong>enter the game theory pattern</strong></p>
<p><span id="more-14"></span>this is was where my idea of a <a href="http://en.wikipedia.org/wiki/Game_theory" title="Game Theory" target="_blank">game theory</a> patterns came into play.  game theory is a branch of applied mathematics, most notably used in social sciences (economics, biology, engineering etc.).  it is also used a bit in computer science, although referred to in that field as artificial intelligence.</p>
<p>thanks <a href="http://wikipedia.org" title="Wikipedia" target="_blank">wikipedia</a> <img src='http://blog.dewaldbotha.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>game theory allows someone to apply a bit of mathematics to capture data from a situation and use that data to build a strategic base, which allows you to make decisions to determine your success.</p>
<p>take for example morning traffic &#8211; you have 2 routes to take to work &#8211; one a bit longer than the other, but the longer route is less likely to be busy.</p>
<p>what do you do?  most of us will take the pro&#8217;s and con&#8217;s of both routes and use that to base our decision upon.</p>
<p>game theory works exactly, well almost, the same.  except you will probably use a bit of math to finalize your decision (average speed, distance etc.)</p>
<p>so why not use game theory in development, especially in a field like mobile, which is very limiting and very frustating at first.  but the good kind of limitation, the kind of limitation that could possibly create innovation.  the guys behind a phenomenon like <a href="http://twitter.com" title="Twitter" target="_blank">twitter</a> decided to limit people to 140 characters.  and despite this limitation, people have found innovative ways to increase the effect of that 140 character limited textual communication.</p>
<p>game theory would allow us to mathematically decide on feature sets, the use of algorythms, objects and could even change the flow of an application.</p>
<p>say on an example mobile application we have 3 major features &#8211; chat, activity, content sharing.</p>
<p>which one do we choose as our killer feature, and which would probably make the application more bloated and more problematic to use.  why not use the main variable in the equation to help you decide &#8211; the user.</p>
<p>i often secretly chuckle to myself, when a system gets developed for one purpose and all the users start using it for a totally different end result, no one really every thought of, or intended for.  you could use this to your advantage with game theory.</p>
<p>a simple case could be made using application flow as an example.  we have a menu with chat, activity feed and shared content.  which one is more important, which one would you make easier to access for the user.  by simply adding a click count to each feature, you could sort a menu by popularity, rather than obvious choice as this will invariably change someday.</p>
<p>if users decide this month, chat is the next best thing, let chat be number one on your menu &#8211; and i know some user interface expert will maintain that consistency is important, but if users consistently choose chat as their most important feature, what would the issue be?</p>
<p>this would change the way you develop and architecture software as well.  since you know chat is gaining in popularity according to your available data, then you should be able to utilize servers better for your chat functionality.  write scaling scripts which would use databases sharding.  and on the other hand, allow your game theory pattern to simplify things &#8211; why use a 40 slave/master instances of a database if all you need is one.  eliminate unnecessary overheads in that way &#8211; more layers makes for more complexity, makes for more overhead, and will eventually kill your application.</p>
<p>i&#8217;ve still got to convince a couple of people to maybe try this game theory pattern approach to development and if, by chance, <a href="http://martinfowler.com/" title="Martin Fowler" target="_blank">martin fowler</a> reads this article:</p>
<p>&#8216;please martin, consider this pattern as a topic for a new book! <img src='http://blog.dewaldbotha.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> &#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dewaldbotha.co.za/2009/02/11/applying-game-theory-patterns-to-development/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>crossing the mvc divide, kohana and zend style</title>
		<link>http://blog.dewaldbotha.co.za/2009/01/23/crossing-the-mvc-divide-kohana-and-zend-style/</link>
		<comments>http://blog.dewaldbotha.co.za/2009/01/23/crossing-the-mvc-divide-kohana-and-zend-style/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 09:36:40 +0000</pubDate>
		<dc:creator>dewaldbotha</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[kohanaphp]]></category>
		<category><![CDATA[model view controller]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://blog.dewaldbotha.co.za/2009-01-23/crossing-the-mvc-divide-kohana-and-zend-style/</guid>
		<description><![CDATA[so, i&#8217;ve been pretty much a zend framework addict, ever since i coded my first bootstrap.  thinking back to that countless hours trying to understand the beast that is zf, ahhh, what fond memories&#8230;  and lately i&#8217;ve also been playing around a bit with kohana, which is another web based mvc framework, but definitely a <a href="http://blog.dewaldbotha.co.za/2009/01/23/crossing-the-mvc-divide-kohana-and-zend-style/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>so, i&#8217;ve been pretty much a <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">zend framework</a> addict, ever since i coded my first bootstrap.  thinking back to that countless hours trying to understand the beast that is <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">zf</a>, ahhh, what fond memories&#8230;  and lately i&#8217;ve also been playing around a bit with <a href="http://kohanaphp.com/" title="kohana" target="_blank">kohana</a>, which is another web based mvc framework, but definitely a bit more lightweight and easier to use than others.</p>
<p>there is however a bit of an issue with <a href="http://en.wikipedia.org/wiki/Model-view-controller#Implementations_of_MVC_as_web-based_frameworks" title="Web based mvc" target="_blank">web based mvc frameworks</a>, and in my opinion a large freaking elephant that no one seems to talk about.  how can i call a controller from within a view and assign an action to it &#8211; and is it correct to do so?</p>
<p>and before the quiet whispers start, i&#8217;d like to note that this does not necessarily go against <a href="http://en.wikipedia.org/wiki/Model-view-controller" title="Model View Controller" target="_blank">mvc</a> principles.  if you think about it &#8211; the whole point of <a href="http://en.wikipedia.org/wiki/Model-view-controller" title="Model View Controller" target="_blank">mvc</a> is to seperate business logic, application logic and the view (presentation logic) &#8211; and i&#8217;ve confirmed this with a couple of conversations with other developers.  but on the other hand, if you feel that i have overstepped a boundary &#8211; please do let me know if there is a better, or more correct way of doing this.</p>
<p><span id="more-11"></span><strong>now lets give a practical problem:</strong></p>
<p>i have a view template, lets call it home, which consists out of a header, navigation, content and footer.  now my content is pretty much controlled by the controller and whatever the view calls to display.  but say for example something you wish to partially include a login form in the top header.</p>
<p>how do i actually use a controller to change the way the view behaves, without building the logic into all the controllers which i access.  now in order to overcome this &#8211; we can attach a controller, lets call it log controller, to the view.  this controller will have the business logic to decide whether or not a login or a logout form must be generated.</p>
<p>now the obvious would be to include a partial view, but then the issue arrives where you let your view start making logic decisions, instead of presentation. on the other hand, you can use the current controller to help you decide this, but if you ask me, that might be a bit of overkill and lots of extra and duplicate coding.</p>
<h3>the solution:</h3>
<p>in <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">zend framework</a>, i found the solution to be a bit easier than in <a href="http://kohanaphp.com/" title="kohana" target="_blank">kohana</a>, but still fun to figure out.</p>
<h4><img src="http://farm4.static.flickr.com/3415/3219206619_3a9927a7ca_o.jpg" alt="Zend Framework" width="429" height="255" /></h4>
<h3><a href="http://framework.zend.com/" title="Zend Framework" target="_blank">zend framework</a>:</h3>
<p>okay, so i have my home template, which is controlled by my home controller.  on my home template in my header i have a form which would change depending on if the user is logged in or not, which i would like to be controlled by my log controller.</p>
<p>so, in my view i would do the following call:<strong><code></code></strong></p>
<p><strong><code>&lt;?php echo $this-&gt;action('log','generateForm') ?&gt;</code></strong></p>
<p>which would go to the log controller and call the method generateForm() &#8211; so in generate form i will be able to decide whether the user is logged in or not and assign the corresponding result to the view assigned to the controller, in the case above it would most likely be the generateForm view.</p>
<p>there you go &#8211; without having to overcomplicate things or duplicate code, you can assign controllers as part of your presentation logic with a simple call.  i have however not yet figured out how to pass variables to the action method you are calling, but as soon as i have an update i will let you know.</p>
<h4><img src="http://farm4.static.flickr.com/3102/3219908744_e4b6f969c4_o.png" alt="Kohana" width="400" height="200" /></h4>
<h3><a href="http://kohanaphp.com/" title="kohana" target="_blank">kohana</a>:</h3>
<p>in <a href="http://kohanaphp.com/" title="kohana" target="_blank">kohana</a> the task is  a bit more complicated &#8211; luckily i found in the underbelly of the <a href="http://forum.kohanaphp.com/" title="kohana" target="_blank">kohana forums</a> an user with the same problem &#8211; which came up with the following solution -</p>
<p><strong><code>&lt;?php<br />
class Dispatch_Core{</code></strong></p>
<p><strong><code>protected $controller;</code></strong></p>
<p><strong><code> public static function controller($controller)</code><br />
<code>{</code><br />
<code>$controller_file=strtolower($controller);</code></strong></p>
<p><strong><code>// Set controller class name</code><br />
<code>$controller = ucfirst($controller).'_Controller';</code></strong></p>
<p><strong><code>if(!class_exists($controller, FALSE))</code><br />
<code>{</code><br />
<code>// If the file doesn't exist, just return</code><br />
<code>if (($filepath = Kohana::find_file('controllers', $controller_file)) === FALSE)</code><br />
<code>return FALSE;</code></strong></p>
<p><strong><code>// Include the Controller file</code><br />
<code>require_once $filepath;</code><br />
<code>}</code></strong></p>
<p><strong><code>// Run system.pre_controller</code><br />
<code>Event::run('dispatch.pre_controller');</code></strong></p>
<p><strong><code>// Initialize the controller</code><br />
<code>$controller = new $controller;</code></strong></p>
<p><strong><code>// Run system.post_controller_constructor</code><br />
<code>Event::run('dispatch.post_controller_constructor');</code></strong></p>
<p><strong><code>return new Dispatch($controller);</code><br />
<code>}</code><br />
<code>public function __construct(Controller $controller)</code><br />
<code>{</code><br />
<code>$this-&gt;controller=$controller;</code></strong></p>
<p><strong><code>}</code></strong></p>
<p><strong><code>public function __get($key)</code><br />
<code>{</code><br />
<code>if($key=='controller')</code><br />
<code>{</code><br />
<code>return $this-&gt;$key;</code><br />
<code>}</code><br />
<code>else</code><br />
<code>{</code><br />
<code>return $this-&gt;controller-&gt;$key;</code><br />
<code>}</code><br />
<code>}</code></strong></p>
<p><strong><code>public function __set($key,$value)</code><br />
<code>{</code><br />
<code>$this-&gt;controller-&gt;$key=$value;</code><br />
<code>}</code></strong></p>
<p><strong><code>public function __toString()</code><br />
<code>{</code><br />
<code>return $this-&gt;render();</code><br />
<code>}</code></strong></p>
<p><strong><code>public function render()</code><br />
<code>{</code><br />
<code>return (string) $this-&gt;controller;</code><br />
<code>}</code></strong></p>
<p><strong><code>public function __call($name,$arguments=null)</code><br />
<code>{</code><br />
<code>if(method_exists($this-&gt;controller,$name))</code><br />
<code>{</code><br />
<code>return $this-&gt;method($name,$arguments);</code><br />
<code>}</code><br />
<code>return false;</code><br />
<code>}</code></strong></p>
<p><strong><code>public function method($method,$arguments=null)</code><br />
<code>{</code><br />
<code>if(!method_exists($this-&gt;controller,$method))</code><br />
<code>return false;</code></strong></p>
<p><strong><code>if (method_exists($this-&gt;controller,'_remap'))</code><br />
<code>{</code><br />
<code>// Make the arguments routed</code><br />
<code>$arguments = array($method, $arguments);</code></strong></p>
<p><strong><code>// The method becomes part of the arguments</code><br />
<code>array_unshift($arguments, $method);</code></strong></p>
<p><strong><code>// Set the method to _remap</code><br />
<code>$method = '_remap';</code><br />
<code>}</code></strong></p>
<p><strong><code>ob_start();</code></strong></p>
<p><strong><code>if(is_string($arguments))</code><br />
<code>{</code><br />
<code>$arguments=array($arguments);</code><br />
<code>}</code></strong></p>
<p><strong><code>switch(count($arguments))</code><br />
<code>{</code><br />
<code>case 1:</code><br />
<code>$result=$this-&gt;controller-&gt;$method($arguments[0]);</code><br />
<code>break;</code><br />
<code>case 2:</code><br />
<code>$result=$this-&gt;controller-&gt;$method($arguments[0], $arguments[1]);</code><br />
<code>break;</code><br />
<code>case 3:</code><br />
<code>$result=$this-&gt;controller-&gt;$method($arguments[0], $arguments[1], $arguments[2]);</code><br />
<code>break;</code><br />
<code>case 4:</code><br />
<code>$result=$this-&gt;controller-&gt;$method($arguments[0], $arguments[1], $arguments[2], $arguments[3]);</code><br />
<code>break;</code><br />
<code>default:</code><br />
<code>// Resort to using call_user_func_array for many segments</code><br />
<code>$result=call_user_func_array(array($this-&gt;controller, $method), $arguments);</code><br />
<code>break;</code><br />
<code>}</code></strong></p>
<p><strong><code>// Run system.post_controller</code><br />
<code>Event::run('dispatch.post_controller');</code></strong></p>
<p><strong><code>if($result!=NULL)</code><br />
<code>{</code><br />
<code>$result=ob_get_contents();</code></strong></p>
<p><strong><code>ob_end_clean();</code><br />
<code>}</code></strong></p>
<p><strong><code>return $result;</code><br />
<code>}</code></strong></p>
<p><strong><code>}</code></strong></p>
<p>now the above you can save under your library directory in your application for ease of use as something like dispatch.php.</p>
<p>so to solve the problem of the login form &#8211; in my default home template i&#8217;ve added <strong><code>&lt;?php echo $this-&gt;template-&gt;partial = new View('log/generateform');?&gt;</code></strong></p>
<p>in the generateform view i called i will use the following:</p>
<p><strong><code>&lt;?php</code><br />
<code>//call the log controller</code><br />
<code>$dispatch = Dispatch::Controller('log');</code><br />
<code>//call the generateForm method</code><br />
<code>$dispatch-&gt;method('generateForm');</code><br />
<code>//return the result from the template to the view</code><br />
<code>echo $dispatch-&gt;template;</code><br />
<code>?&gt;</code></strong></p>
<p>the above will call the  log controller and the method generateForm, in the controller, you can specify the template, for e.g. <strong><code>&lt;?php public $template = 'kohana/partial/navigation';?&gt;</code></strong> and then in the action / controller assign any values to be used by the template view/presentation.</p>
<p>oh, and one more cool thing with the above is that you can also call the method using parameters for e.g. <strong><code>&lt;?php $dispatch-&gt;generateForm(array('title'='login'));?&gt;</code></strong></p>
<h3>recap</h3>
<p>as i said in the beginning, if anyone feels that i&#8217;ve overstepped the <a href="http://en.wikipedia.org/wiki/Model-view-controller" title="Model View Controller" target="_blank">mvc</a> divide, please let me know, i&#8217;m sure that with the assigning of a controller/action from within a view is totally acceptable, since only your presentation logic is affected by it really, and the controller still decides on what is available for the view to pull, without having adjust your controllers to cater for everything.</p>
<p>and if someone knows of a quicker method in <a href="http://kohanaphp.com/" title="kohana" target="_blank">kohana</a>, also do let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dewaldbotha.co.za/2009/01/23/crossing-the-mvc-divide-kohana-and-zend-style/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
